checking whether the current object is a specific class.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
M

Mr. X.

Hello.
I need to check whether the current object Is a specific class,

For example :
function a(b as object)
' I need to check here, whether b is Button class.
' How can I do that ?
end function

Thanks :)
 
Am 10.06.2010 21:36, schrieb Mr. X.:
Hello.
I need to check whether the current object Is a specific class,

For example :
function a(b as object)
' I need to check here, whether b is Button class.
' How can I do that ?
end function

Thanks :)

What's your intention?
 
Hello.
I need to check whether the current object Is a specific class,

For example :
function a(b as object)
' I need to check here, whether b is Button class.
' How can I do that ?
end function

Thanks :)

Use:

If TypeOf b Is Button Then
' .....
End If

HTH,

Onur Güzel
 
I need to check whether the current object Is a specific class,
For example :
function a(b as object)

Why?
Bear with me: It's not as silly a question as it sounds; it depends on
what you want to /do/ with it once you've found it.

(1) If you want a method that, say, enables or disables the control that
you pass to it, but you want it to work for lots of different Types of
Control (contrived, but a common thing to do), use overloading:

Function Z( byval btn as Button, byval enabled as Boolean )
Function Z( byval tb as TextBox, byval enabled as Boolean )
Function Z( byval cmb as ComboBox, byval enabled as Boolean )

When you code
Z( Me.Button3 )
the compiler works out /which/ method to call.

(2) If you need to work out what Type of control you've got in, say, an
Event Handler, use TypeOf:

Sub Thing_Click( byval sender as Object, byval e as EventArgs ) _
Handles ...
If TypeOf sender Is TextBox Then
With DirectCast( sender, textBox )
. . .
End With
End If
End Sub

If you're testing for lots of Types, you still need a chain of If ..
Else's; you can't use TypeOf with Select .. Case.

(3) If you need to detect subclasses of a given Type, things get a bit
more fiddly:

If thing.GetType().IsSubClassOf( SomeType ) Then
' thing is a subclass of SomeType
' but not SomeType itself.
. . .
End If

HTH,
Phill W.
 
Why?
Bear with me: It's not as silly a question as it sounds; it depends on
what you want to /do/ with it once you've found it.

(1) If you want a method that, say, enables or disables the control that
you pass to it, but you want it to work for lots of different Types of
Control (contrived, but a common thing to do), use overloading:

Function Z( byval btn as Button, byval enabled as Boolean )
Function Z( byval tb as TextBox, byval enabled as Boolean )
Function Z( byval cmb as ComboBox, byval enabled as Boolean )

No need for overloading in this case. Just set the parameter type as
Control. The Enabled property is inherited from Control:

Private Sub Z(ByVal c As Control, ByVal enabled As Boolean)
c.Enabled = enabled
End Sub

Chris
 
Back
Top