type is not defined

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
I have a method/sub that takes an object as a parameter, then it scans through a panels controls and trys to find if it is in the window.

VB.NET:
Public Sub removetype (ByVal findobject As Object)
        Dim types As System.Type = findobject.GetType
        For Each ThisCtrl In Flow.Controls           'flow is a panel
            Console.WriteLine("scanning...")
            If TypeOf ThisCtrl Is types Then         ' type types is not defined
                Console.WriteLine("found: " & ThisCtrl.GetType().ToString)
                Try
                    Flow.Controls.Remove(ThisCtrl)
                    Return
                Catch ex As Exception
                    Console.WriteLine("not removed")
                End Try
            End If
        Next
        Console.WriteLine("...done scan")
    End Sub

but yet it gives me a type 'types' is not define
 
Last edited:
In your code, 'types' is not a data type. It's a variable of type System.Type. They are not the same thing. TypeOf requires a data type, e.g. String or Integer. If you want to compare to a System.Type object then you must use another System.Type object:
VB.NET:
If ThisCtrl.GetType() Is types Then
On a different note, if your variable refers to a single type, why is it named 'types'?
 
thanks it works, i didn't realize that thats what typeof did. I set the variable to 'types' because I didn't think i would be able to use type as a variable name, but you actually can. thanks again!
 
'Type' is the name of a class. It's only reserved words, i.e. those that turn blue in the code editor, that are an issue being used as identifiers. Even then, you can use a reserved word if you wrap it in brackets, e.g.
VB.NET:
Dim [end] As DateTime
That should generally be avoided though.
 
Back
Top