free geoip VB.Net Equivalent to C# 'As' Keyword -- TryCast - protected virtual void jaysonBlog
in

jaysonKnight.com

Welcome to my corner of the internet

protected virtual void jaysonBlog

A conduit to the voices in 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.

Share this post: Submit VB.Net+Equivalent+to+C%23+%27As%27+Keyword+--+TryCast to Technorati | Submit VB.Net+Equivalent+to+C%23+%27As%27+Keyword+--+TryCast to del.icio.us | Submit VB.Net+Equivalent+to+C%23+%27As%27+Keyword+--+TryCast to digg.com | Submit VB.Net+Equivalent+to+C%23+%27As%27+Keyword+--+TryCast to reddit.com | Submit VB.Net+Equivalent+to+C%23+%27As%27+Keyword+--+TryCast to DotNetKicks | Add VB.Net+Equivalent+to+C%23+%27As%27+Keyword+--+TryCast to Live Bookmarks

Comments

 

Erik Lane said:

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

March 9, 2007 8:25 AM
 

J-O Eriksson said:

Nice one Jayson!

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

March 9, 2007 9:57 AM
 

DotNetKicks.com said:

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

March 9, 2007 8:24 PM
 

Jakub said:

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

July 24, 2007 9:51 AM
 

Chris said:

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

December 5, 2007 6:37 PM
 

Wuvist said:

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

April 2, 2008 8:06 PM
 

Ryan said:

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

April 3, 2008 12:27 PM
 

wendy said:

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.

May 7, 2008 7:36 AM
 

John Nolan said:

Thanks you've just saved me 20 mins too.

June 20, 2008 1:10 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About Jayson Knight

Jayson Knight was clueless to the computer programming world until he took a C++ class in college. The rest is proverbial history. He has been building applications targeting the .Net framework for 7 years, focusing mainly on internet technologies and database driven web application development.

Most recently he left the world of Corporate IT to finish up his degree in Chemistry, with an eye on Medical School and an Anesthesiology residency program. Read this post for more information.

He is also a Community Server MVP: Community Server is the software that runs this site, plus many others on the web. For more information, check out http://csmvps.com.

When he finds time to pry himself away from his computer and university studies, he can be found on the mountain bike trails when it's warm, and on the ski slopes when it's cold.

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

Terms of Service/Privacy Policy