{"id":6803,"date":"2006-06-26T20:55:00","date_gmt":"2006-06-26T20:55:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2006\/06\/26\/vb-curioddities-1-enum-enum-my-kingdom-for-an-enum-parse\/"},"modified":"2024-07-05T14:52:28","modified_gmt":"2024-07-05T21:52:28","slug":"vb-curioddities-1-enum-enum-my-kingdom-for-an-enum-parse","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/vb-curioddities-1-enum-enum-my-kingdom-for-an-enum-parse\/","title":{"rendered":"VB Curioddities #1: Enum, Enum, my kingdom for an Enum.Parse"},"content":{"rendered":"<p><font face=\"Arial\" size=\"2\">Hey folks, my name&#8217;s Kit George and I&#8217;ve joined the VB team from the CLR. VB is after all, the best language, so of course, it makes sense to work directly on it!<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">Like all languages, VB has it&#8217;s little &#8216;oddities&#8217;, so i thought i would start a series to present a few of these. These are little (and perhaps large) curious VB things that you may or may not have noticed. Either way, you can bring them up at the water cooler to impress everyone with your VB knowledge.<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">I thought I would start with an interesting oddity&nbsp;surrounding Enum.Parse. Enum is of course, a keyword in Visual Basic, since it was already a keyword in days prior to .NET. This creates an interesting conflict between the Enum class (Enum is itself, a class in the System namespace of .NET), and the keyword. Why it becomes interesting is that the Enum class has some Shared members on it, most interestingly, Parse. You would normally invoke such a method with code like this:<\/font><\/p>\n<p>&nbsp; <font face=\"Courier New\" size=\"2\">Public Class Test<br \/>&nbsp; Public shared Sub Main()<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&nbsp;&nbsp;&nbsp; Dim ct as CarType = <font color=\"#ff0000\">Enum<\/font>.Parse(GetType(CarType), &#8220;Sedan&#8221;)<br \/>&nbsp;&nbsp;&nbsp; MsgBox(ct)<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&nbsp; End Sub<br \/>&nbsp;End Class<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&nbsp;Enum CarType<br \/>&nbsp; Sedan = 1<br \/>&nbsp; Sports = 2<br \/>&nbsp; SUV = 3<br \/>&nbsp;End Enum<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">However, because Enum is a keyword, you&#8217;ll get an exception on the above code because the &#8216;Enum.Parse&#8217; line is treated as if you&#8217;re trying to declare an Enum by the compiler. You could workaround this easily by simply doing the followin. The square brackets around the name Enum tell the compiler to NOT treat it like a keyword, therefore, it treats it like a class call:<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&nbsp;&nbsp;&nbsp; Dim ct as CarType = <font color=\"#0000ff\">[Enum]<\/font>.Parse(GetType(CarType), &#8220;Sedan&#8221;)<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">But in addition to the above syntax, and in order to make it even simpler, VB allows you to use the actual name of the Enum (CarType in this case) to invoke the Parse method. To all intents and purpose, this LOOKS like a Shared method call, so it should be supported:<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&nbsp;&nbsp;&nbsp; Dim ct as CarType = <font color=\"#0000ff\">CarType<\/font>.Parse(GetType(CarType), &#8220;Sedan&#8221;)<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">Now here&#8217;s where it gets interesting. Under the hood, the above line is actually turned into a call through to [Enum].Parse. That is, the CarType.Parse call has nothing to do with the CarType Enum itself, instead, it simply becomes a standard [Enum].Parse call. Therefore, what if I did something like this:<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&nbsp;Enum BoatType<br \/>&nbsp;&nbsp;Trawler = 1<br \/>&nbsp;&nbsp;SpeedBoat = 2<br \/>&nbsp;&nbsp;CruiseShip = 3<br \/>&nbsp;End Enum<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&nbsp;&nbsp;&nbsp; Dim ct as CarType = <font color=\"#0000ff\">BoatType<\/font>.Parse(GetType(CarType), &#8220;Sedan&#8221;)<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">Surely this wouldn&#8217;t compile, right? After all, it looks like we&#8217;re parsing&nbsp;on the BoatType class, into a CarType type: that surely doesn&#8217;t work.<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">But remember that CarType.Parse simply got turned into [Enum].Parse? Well, ANY other Enum call is the same. So BoatType.Parse, really just becomes [Enum].Parse. And this works just fine! So the above code compiles and runs just fine. Of course, I would not suggest under any circumstances, that you write your code this way, it is odd to read.<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">Note that of course, this will NOT work:<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&nbsp;&nbsp;&nbsp; Dim bt as <font color=\"#ff0000\">BoatType<\/font> = <font color=\"#000000\">BoatType.<\/font>Parse(GetType(<font color=\"#ff0000\">CarType<\/font>), &#8220;Trawler&#8221;)<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">The reason is that the KEY things for the transformation to succeed&nbsp;are that a) the type on the left of the assignment, and the type in the first parameter to the Parse method, match (they don&#8217;t in the above case, which is what causes it to fail), and b) that the string can be converted to the specified type (That is, it is a member of the specified Enum. In the above case it can, but of course, we&#8217;re gonna fail here because of item a) anyway).<\/font><\/p>\n<p><font face=\"Arial\" size=\"2\">At the end of the day, my own preference IS to use the name of the specific Enum, but simply use the same name as the Enum I&#8217;m parsing into. But I do like messing with people from time to time with this one ;-).<\/font><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey folks, my name&#8217;s Kit George and I&#8217;ve joined the VB team from the CLR. VB is after all, the best language, so of course, it makes sense to work directly on it! Like all languages, VB has it&#8217;s little &#8216;oddities&#8217;, so i thought i would start a series to present a few of these. [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[21,195],"tags":[90,165],"class_list":["post-6803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-im-a-vb","category-visual-basic","tag-kit-george","tag-vb2005"],"acf":[],"blog_post_summary":"<p>Hey folks, my name&#8217;s Kit George and I&#8217;ve joined the VB team from the CLR. VB is after all, the best language, so of course, it makes sense to work directly on it! Like all languages, VB has it&#8217;s little &#8216;oddities&#8217;, so i thought i would start a series to present a few of these. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/6803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=6803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/6803\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=6803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=6803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=6803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}