Scott Hanselman

The VB equivalent to C# typeof() keyword

August 31, 2007 Comment on this post [12] Posted in Learning .NET
Sponsored By

This post is so I can search my blog for it later. For whatever reason I always forget this and find myself trying to remember what...

typeof(Foo)

...is in VB...it's...

GetType(Foo)

Sigh. Now I won't forget. I wish I know why that one always flummoxes me.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
August 31, 2007 3:51
To make it even more confusing for those that go back and forth:

VB's global GetType method is equivalent to C#'s typeof operator
VB's TypeOf ... Is operator is equivalent to C#'s "is" operator
and of course, "Is" in VB is "==" in C#...

(from http://tinyurl.com/2fydtz)
August 31, 2007 4:26
Wouldn't Foo.GetType() do the same thing?
August 31, 2007 4:41
Yes, but that's a runtime thing and TypeOf(Foo) is a compile-time thing.
August 31, 2007 4:58
Code DOM can be handy in these situations.

CodeDomProvider.CreateProvider("VB").GenerateCodeFromExpression(new CodeTypeOfExpression("Foo"), Console.Out, new CodeGeneratorOptions());
August 31, 2007 9:34
Not certain if this is worth noting here but there seems to be some semantic difference between Type.GetType(), typeof() and Object.GetType() in C# (I'll have to look at them in Reflector) but I was tripped up by some weirdness between them a while back. This is likely related solely to generation of type libraries when using COM Interop. Now I just stick with typeof() and haven't experienced any problems (when using COM Interop or otherwise).
JH
August 31, 2007 10:36
We were just discussing this the other day, thanks for the answer :)
August 31, 2007 14:38
To translate C# to VB, I use SharDevelop. It is fine .. and free.
Either I copy some piece of code in a new class file or I open a C# file or I open a C# projet.
Yes you can convert a whole projet.
Never I understand why Microsoft did not do the same in DotNet.
Cheers
August 31, 2007 17:08
To make things even more confusing, VB also supports the following:

If TypeOf obj Is TextBox Then
August 31, 2007 20:41
Just add anything you need to remember as a code snippet. What if you don't have access to your blog and you need it?
September 03, 2007 12:47
I am guessing you have a very good reason for jumping between VB.NET and C#.NET. I prefer do develop in C#.NET only, all our company's source has been converted to C#.NET from VB6 and all new development is done in C#.NET only. I guess we were just lucky to be allowed to do this.
September 04, 2007 20:14
Add this tidbit to your "Solution Log".
I am sorry I can't remember where I read about this...so I don't have a link.

The concept is to keep a list of problems with their solutions for things you repeatedly encounter, especially the ones that are encountered infrequently enough that one forgets they have already solved them.

We keep ours on our wiki. Then, when I hit a problem, and have that "I know I had a solution to this before" thought...I search the solution log wiki page.

I suppose it could be a text file, or even your blog I guess...perhaps you might want to tag this post with "SolutionLog" so you can find it more easily next time?

-james

ps: your blog & podcast rule
September 08, 2007 22:28
I know exactly what you mean.

I wrote this post a few months ago:
http://www.chrismay.org/2007/04/06/TypeOf+VBNET+Vs+C.aspx

If you are Microsoft, why do you create a function "TypeOf" that has totally differnet applications in VB and C#?

Typeof in C# is like GetType in VB, where VB uses TypeOf to see if 2 types are the same or check for interface implementation.

Kinda dumb.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.