Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Lets say I want to find out the Type of an Object - I often use tags. In order to do this do I really have to instantiate another new object to compare the tag object to? for instance

 

'Lets say obj is a Tag object imported into the function
Private Function IsObjMyClass(obj as Object) as boolean
       Dim MC1 As New MyClass1  'Do I really need to create an object here?
  
       If Object.ReferenceEquals(obj.GetType(), MC1.GetType()) Then
          Return True
       Else
          Return False
       End If
End Function
 

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Posted

TypeOf operator

 

Use the TypeOf operator:

 

'Lets say obj is a Tag object imported into the function
Private Function IsObjMyClass(obj as Object) as boolean
       If TypeOf obj Is MyClass1 Then
          Return True
       Else
          Return False
       End If
End Function

 

Or simpler:

 

'Lets say obj is a Tag object imported into the function
Private Function IsObjMyClass(obj as Object) as boolean
       Return (TypeOf obj Is MyClass1)
End Function

 

Good luck :cool:

Never trouble another for what you can do for yourself.
  • Leaders
Posted

There is one difference between the two methods:

If Object.ReferenceEquals(obj.GetType(), MC1.GetType()) Then
   'obj is an instance of MyClass1
End If
If TypeOf obj Is MyClass1 Then
   'obj is instance of MyClass1 or a derived class
End If

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...