The VB equivalent to C# typeof() keyword
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.
About Newsletter
Code DOM can be handy in these situations.
CodeDomProvider.CreateProvider("VB").GenerateCodeFromExpression(new CodeTypeOfExpression("Foo"), Console.Out, new CodeGeneratorOptions());
CodeDomProvider.CreateProvider("VB").GenerateCodeFromExpression(new CodeTypeOfExpression("Foo"), Console.Out, new CodeGeneratorOptions());
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).
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
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
To make things even more confusing, VB also supports the following:
If TypeOf obj Is TextBox Then
If TypeOf obj Is TextBox Then
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?
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.
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
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
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.
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.
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)