By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Getting object type without creating object

P: n/a
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/..
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"Raghu" <Ra***@nospamzzzqcsi.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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/..

Nov 21 '05 #2

P: n/a
* "Raghu" <Ra***@nospamzzzqcsi.com> scripsit:
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.


GetType Operator
<URL:http://msdn.microsoft.com/library/en-us/vblr7/html/vaoprgettype.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3

P: n/a
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/..

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2o************@uni-berlin.de...
* "Raghu" <Ra***@nospamzzzqcsi.com> scripsit:
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.


GetType Operator
<URL:http://msdn.microsoft.com/library/en-us/vblr7/html/vaoprgettype.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4

P: n/a
* "Raghu" <Ra***@nospamzzzqcsi.com> scripsit:
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)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

P: n/a
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" <Ra***@nospamzzzqcsi.com> wrote in message
news:Oj****************@TK2MSFTNGP12.phx.gbl... 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/..

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2o************@uni-berlin.de...
* "Raghu" <Ra***@nospamzzzqcsi.com> scripsit:
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.


GetType Operator
<URL:http://msdn.microsoft.com/library/en-us/vblr7/html/vaoprgettype.asp>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #6

P: n/a
Thank you guys.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ep**************@TK2MSFTNGP12.phx.gbl...
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" <Ra***@nospamzzzqcsi.com> wrote in message
news:Oj****************@TK2MSFTNGP12.phx.gbl...
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/..

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2o************@uni-berlin.de...
* "Raghu" <Ra***@nospamzzzqcsi.com> scripsit:
> 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.
GetType Operator

<URL:http://msdn.microsoft.com/library/en-us/vblr7/html/vaoprgettype.asp>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.