First time here? Check out the site's "greatest hits" or read a post from the archives. Feel free to leave a comment or ask a question, and consider subscribing to the latest posts via RSS or e-mail. Thanks for visiting!
« 5:01 Developers, Family, and Excitement ... | Main | Fixing Instance Failure when connecting ... »

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.



Thursday, August 30, 2007 3:51:02 PM (Pacific Standard Time, UTC-08:00)
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)
Thursday, August 30, 2007 4:26:57 PM (Pacific Standard Time, UTC-08:00)
Wouldn't Foo.GetType() do the same thing?
Thursday, August 30, 2007 4:41:35 PM (Pacific Standard Time, UTC-08:00)
Yes, but that's a runtime thing and TypeOf(Foo) is a compile-time thing.
Thursday, August 30, 2007 4:58:12 PM (Pacific Standard Time, UTC-08:00)
Code DOM can be handy in these situations.

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

If TypeOf obj Is TextBox Then
Friday, August 31, 2007 8:41:01 AM (Pacific Standard Time, UTC-08:00)
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 12:47:59 AM (Pacific Standard Time, UTC-08:00)
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 8:14:09 AM (Pacific Standard Time, UTC-08:00)
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 10:28:24 AM (Pacific Standard Time, UTC-08:00)
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.

Contact

Sponsors

Hosting By

On this page...

Tags

Calendar

<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Archives

Google Ads