Home All Groups Group Topic Archive Search About

checking whether the current object is a specific class.

Author
10 Jun 2010 7:36 PM
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 :)

Author
10 Jun 2010 7:55 PM
Armin Zingler
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
Author
10 Jun 2010 8:03 PM
Onur_Güzel
On Jun 10, 10:36 pm, "Mr. X." <nospam@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
Author
10 Jun 2010 9:13 PM
Mr. X.
Thanks :)

Show quoteHide quote
"Onur Güzel" <kimiraikkone***@gmail.com> wrote in message
news:f75ba3ec-eefe-411a-84eb-1e96c20702c1@c33g2000yqm.googlegroups.com...
> On Jun 10, 10:36 pm, "Mr. X." <nospam@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
Author
11 Jun 2010 10:50 AM
Phill W.
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.
Author
11 Jun 2010 3:52 PM
Chris Dunaway
Show quote Hide quote
On Jun 11, 5:50 am, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:
> 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 )
>

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