Scott Hanselman

The VB equivalent to C# typeof() keyword

August 30, '07 Comments [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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Thursday, August 30, 2007 11:51:02 PM UTC
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)
Friday, August 31, 2007 12:26:57 AM UTC
Wouldn't Foo.GetType() do the same thing?
Friday, August 31, 2007 12:41:35 AM UTC
Yes, but that's a runtime thing and TypeOf(Foo) is a compile-time thing.
Friday, August 31, 2007 12:58:12 AM UTC
Code DOM can be handy in these situations.

CodeDomProvider.CreateProvider("VB").GenerateCodeFromExpression(new CodeTypeOfExpression("Foo"), Console.Out, new CodeGeneratorOptions());
Friday, August 31, 2007 5:34:55 AM UTC
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).
Friday, August 31, 2007 6:36:43 AM UTC
We were just discussing this the other day, thanks for the answer :)
Friday, August 31, 2007 10:38:56 AM UTC
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
Dominique Gratpain
Friday, August 31, 2007 1:08:55 PM UTC
To make things even more confusing, VB also supports the following:

If TypeOf obj Is TextBox Then
Friday, August 31, 2007 4:41:01 PM UTC
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?
abdu
Monday, September 03, 2007 8:47:59 AM UTC
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.
Marthinus
Tuesday, September 04, 2007 4:14:09 PM UTC
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
James McFarland
Saturday, September 08, 2007 6:28:24 PM UTC
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.