instance of?

  • Thread starter Thread starter Jarod_24
  • Start date Start date
J

Jarod_24

I got a object of type 'Control' passed as a argument to a subroutine of
mine. The object is either a Combobox or a TextBox.

I thought the .GetType could be used to check what type of object it was,
but i haven't had any luck.

In Java you got the instanceOf() operator to do this. What's the equilant in
VB.net?
 
Jarod,
Use TypeOf Is:

Dim c As control

If TypeOf c Is ComboBox Then
Dim combo As ComboBox = DirectCast(c, ComboBox)

ElseIf TypeOf c Is TextBox Then
Dim combo As TextBox = DirectCast(c, TextBox )
End If

You can use Object.GetType along with the GetType function, if you want to
know specifically if its the type. I prefer the TypeOf operator as it
supports classes derived from the listed class.

If c.GetType() Is GetType(ComboBox) Then
Dim combo As ComboBox = DirectCast(c, ComboBox)

ElseIf c.GetType() Is GetType(TextBox) Then
Dim combo As TextBox = DirectCast(c, TextBox )
End If

Hope this helps
Jay
 
You can use GetType but, as the name implies, this returns the type not the
string name of the type so you could do this:

If obj.GetType.Name.toString = "ComboBox" Then

End If
 
* "Jarod_24 said:
I got a object of type 'Control' passed as a argument to a subroutine of
mine. The object is either a Combobox or a TextBox.

I thought the .GetType could be used to check what type of object it was,
but i haven't had any luck.

In Java you got the instanceOf() operator to do this. What's the equilant in
VB.net?

\\\
If TypeOf ... Is ComboBox Then
...
End If
///
 
Back
Top