free geoip VB.Net Equivalent to C# 'As' Keyword -- TryCast - Jayson's Blog - jaysonKnight.com
jaysonKnight.com
Welcome to my corner of the internet

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 Mar 09 2007, 01:43 AM by Jayson Knight

10 Comments

Erik Lane wrote re: VB.Net Equivalent to C# 'As' Keyword -- TryCast
on 03-09-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 03-09-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 03-09-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 07-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 12-05-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 04-02-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 04-03-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 05-07-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 06-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 10-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!

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

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

Terms of Service/Privacy Policy