free geoip VB.Net Equivalent to C# 'As' Keyword -- TryCast - Jayson's Blog - jaysonKnight.com
jaysonKnight.com
A conduit to the voices inside my head.
VB.Net Equivalent to C# 'As' Keyword -- TryCast

[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] 

I've been 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.


Posted Fri, Mar 9 2007 1:43 AM by Jayson Knight
comments powered by Disqus

Comments

Erik Lane wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Fri, Mar 9 2007 8:25 AM

Not a silly post.  Just lets the rest of us average Joe's know that you're human too.  :-)

J-O Eriksson wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Fri, Mar 9 2007 9:57 AM

Nice one Jayson!

I actually was looking for the same thing the other day, no I don't have to look anymore. :-)

DotNetKicks.com wrote VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Fri, Mar 9 2007 8:24 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Jakub wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Tue, Jul 24 2007 9:51 AM

I was exactly in the same situation as you Jayson. Thanks.

Chris wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Wed, Dec 5 2007 6:37 PM

VB's Iif is non-shortcircuiting.  The c# "?:" operator is.  So, they're not *quite* the same.

Wuvist wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Wed, Apr 2 2008 8:06 PM

haha~ Not a silly post... Exactly told me what I needed~

Ryan wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Thu, Apr 3 2008 12:27 PM

VB does indeed support shortcircuiting now, via the new If operator: If(expression, truepart, falsepart). Not to be confused with IIF!

wendy wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Wed, May 7 2008 7:36 AM

thanks Jayson! its just what i was looking for.

ps: i find coding in vb.net cumbersome especially after avoiding in uni and all my work life until now.

John Nolan wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Fri, Jun 20 2008 1:10 AM

Thanks you've just saved me 20 mins too.

Owen wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Wed, Oct 15 2008 1:42 AM

Just for the record... Sharp Develop (free) can convert code from C# to VB.NET and vice versa.  And the code does not need to be compiled first.  It's VERY VERY HANDY!

Bill wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Mon, Dec 15 2008 7:05 AM

Quote:

Perhaps, but if it took me 20 minutes to find it, hopefully this'll save someone else having to do as much digging.

Thanks, you did.

Nancy wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Mon, Aug 9 2010 10:45 PM

Not a silly post.. It actually saved my 20 min :)

rak wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Wed, Sep 22 2010 11:20 PM

we want a table type chart for vb and c# words can you provide it

Azhar wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on Wed, Feb 9 2011 9:28 PM

It definitely saved me a lot of time. Was able to find this post in less than a second. :) Thanks.

Copyright © :: JaysonKnight.com
External Content © :: Respective Authors

Terms of Service/Privacy Policy