Comparing Types

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Why won't either one of the below If expressiions evaluate to TRUE:

Dim ob As Object
ob = "This String"
If ob.GetType Is Type.GetType("String") Then
.....
End If
If ob.GetType is Type.GetType("Object) then
....
End If

I guess I just don't understand why a type isn't a type!
 
Dennis,
Because "String" & "Object" are not the names of qualified types.
Type.GetType requires a qualified type name, such as "System.String" &
"System.Object", or "MyProject.MyType, MyAssembly".

In other words Type.GetType("String") returns nothing, while
Type.GetType("System.String") returns the System.Type object for the String
class.

However: I normally use:

If TypeOf ob Is String Then
...
End If

If TypeOf ob Is Object Then
...
End If

Hope this helps
Jay
 
Yes, it helps a lot. Thanks Jay. Sometimes I feel so stupid with these
simple questions. vb.net just has too many types, gettypes, type.gettype,
typeof, etc.
 
Dennis,
I would use GetType (as you originally asked) when I want to check a
variable *is specifically* a specific type, while I use TypeOf to check a
variable *is generally* a type. By *is generally* I mean if it is that type,
inherits from the type or implements the type.

For example:

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top