From: Mr. X. on
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 :)
From: Armin Zingler on
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?

--
Armin
From: Onur Güzel on
On Jun 10, 10:36 pm, "Mr. X." <nospam(a)nospam_please.com> wrote:
> 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
From: Mr. X. on
Thanks :)

"Onur G�zel" <kimiraikkonen85(a)gmail.com> wrote in message
news:f75ba3ec-eefe-411a-84eb-1e96c20702c1(a)c33g2000yqm.googlegroups.com...
> On Jun 10, 10:36 pm, "Mr. X." <nospam(a)nospam_please.com> wrote:
>> 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

From: Phill W. on
On 10/06/2010 20:36, Mr. X. wrote:

> 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.
 |  Next  |  Last
Pages: 1 2
Prev: Datagridview selected rows
Next: Inheritance and COM