[Update] Apparently the TryCast keyword was introduced in VB 2005. That miffs me a little. Actually, that miffs me a lot since this project is on .Net 1.1 with no plans to move to 2.0 any time soon. [/update]
plagued blessed by inheriting a VB.Net app as one of my projects at work. I am not a language snob; I have nothing against VB (it's all IL in the end, right?) but sometimes it's a little slow going when I have to spend time looking up equivalent VB jargon for stuff that I inherently know in C#. The point is that I haven't done any coding in VB in 3-4 years, so C# just comes more natural and I spend a bit of time hitting the docs for VB syntax related stuff.
I won't get into a long boring post about the benefits of the 'as' keyword in C#...the audience of this blog should already know when/where to use it. I needed the same functionality in VB so I set out to find the equivalent construct. Try searching for "C# as VB equivalent" and related derivatives and you'll get the phonebook on language comparisons (some of them even providing a table with a line item list of C# to VB conversions for syntax, keywords, etc), but nothing directly mentioning the VB equivalent.
After 10 minutes or so of looking around, I found it: In VB you want to use the TryCast function. Easy enough. Silly post? Perhaps, but if it took me 20 minutes to find it, hopefully this'll save someone else having to do as much digging.
Sidenote: VB.Net also supports the C# ternary operator (which of course is very handy for doing null assignation tests) via the IIf function in the Microsoft.VisualBasic namespace.
Fri, Mar 9 2007 1:43 AM