PC Review


Reply
Thread Tools Rate Thread

checking whether the current object is a specific class.

 
 
Mr. X.
Guest
Posts: n/a
 
      10th Jun 2010
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
 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      10th Jun 2010
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
 
Reply With Quote
 
Onur Güzel
Guest
Posts: n/a
 
      10th Jun 2010
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
 
Reply With Quote
 
Mr. X.
Guest
Posts: n/a
 
      10th Jun 2010
Thanks

"Onur Güzel" <(E-Mail Removed)> wrote in message
news:f75ba3ec-eefe-411a-84eb-(E-Mail Removed)...
> 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


 
Reply With Quote
 
Phill W.
Guest
Posts: n/a
 
      11th Jun 2010
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.
 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      11th Jun 2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checking object is of type parent class Tarscher Microsoft C# .NET 6 31st Oct 2008 10:35 AM
Question regarding initializing base class object to derived class object, archana Microsoft C# .NET 1 9th Jan 2007 07:50 AM
How to determine if an object class is indirectly inherited from a specific type? Joergen Bech Microsoft VB .NET 2 19th Aug 2005 01:17 PM
Want to take the path of specific overloaded constructors from derived class up to just one below the base class. hazz Microsoft C# .NET 3 15th Aug 2004 01:59 PM
class object current dir Ron Microsoft VB .NET 1 27th Jan 2004 09:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:57 AM.