Getting object type without creating object

  • Thread starter Thread starter Raghu
  • Start date Start date
R

Raghu

In C#, the typeof keyword can be used to get a type of the class. This does
not require object to be created first. However O am not sure how do the
same thing in vb. I don't want to create the object just to get its type. I
would appreciate if any one can can show me how to do this in vb.net.

Thanks.
Raghu/..
 
Raghu,
Use the GetType operator, instead of the GetType method.

Dim t As Type = GetType(Integer)

If you want to check an object variable for a specific type you can use the
TypeOf Is operator.

Dim o As Object
If TypeOf o Is Integer Then
' we have an Integer here!
End If

Hope this helps
Jay
 
I did not make my self clear. I can write following code in csharp:

public class MyAgent {
...
}

Type t = typeof(MyAgent);

When I write following vb.net code, it fails to compile:

Public Class MyAgent
....
End Class

Dim t as Type

t = Type.GetType(MyAgent)

However I can write following code, but it does not catch compile time error
if class name is changed:

t = Type.GetType("MyAgent")

Any ideas?

Thanks.
Raghu/..
 
* "Raghu said:
I did not make my self clear. I can write following code in csharp:

public class MyAgent {
...
}

Type t = typeof(MyAgent);

When I write following vb.net code, it fails to compile:

Public Class MyAgent
...
End Class

Dim t as Type

t = Type.GetType(MyAgent)

However I can write following code, but it does not catch compile time error
if class name is changed:

t = Type.GetType("MyAgent")

Use 't = GetType(MyAgent)'.
 
Raghu,
I gave you an example, Herfried gave you the documentation.

Use the GetType operator, not the Type.GetType method!
Dim t as Type

t = GetType(MyAgent)

Hope this helps
Jay

Raghu said:
I did not make my self clear. I can write following code in csharp:

public class MyAgent {
...
}

Type t = typeof(MyAgent);

When I write following vb.net code, it fails to compile:

Public Class MyAgent
...
End Class

Dim t as Type

t = Type.GetType(MyAgent)

However I can write following code, but it does not catch compile time error
if class name is changed:

t = Type.GetType("MyAgent")

Any ideas?

Thanks.
Raghu/..
 

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