Welcome to AddressOf.com Sign in | Help

VB.NET Whidbey - The TryCast operator.

Introducing the TryCast operator (similar to the as operator in C#).  Paul Vick explains the specifics on this and I urge you to visit his site for further details.  Suffice to say, this on is definitely on my list of tools I'll be adding to my arsenal.  However, it does bring up an interesting question (or two).

We will have the following operators:

CType
DirectCast
TryCast

Looks like one of those is out of place, doesn't it?  Wouldn't the following list look nicer?

Cast
DirectCast
TryCast

It seems that it would be simple enough to alias Cast with CType and give the developer a choice between which one they want to use.

And while we're at it, how about the TryDirectCast operator?

Really, the point to this is to ask how the decision process works when deciding what to call new keywords in VB.NET.  Paul stated in a comment on his blog that he has thought about discussing that very subject, so stay tuned ;-)

Published Wednesday, March 03, 2004 12:07 AM by CorySmith
Filed under:

Comments

# Take Outs for 3 March 2004

Thursday, March 04, 2004 2:13 AM by Enjoy Every Sandwich
Take Outs for 3 March 2004

# re: VB.NET Whidbey - The TryCast operator.

Tuesday, March 09, 2004 1:19 PM by Mathew Nolton
Less is more.

the only advantage of using the 'as' keyword is that it creates different IL code so that there is no catching of an exception (e.g. the usual try/catch approach to casting) which means the performance is slightly better. if it wasn't for this slight performance improvement i wouldn't see the benefit. does vb.net promise the same?

-Mathew Nolton

# re: VB.NET Whidbey - The TryCast operator.

Saturday, March 13, 2004 4:49 AM by Cory Smith
To my knowledge, that is exactly that the TryCast operator does... it's identical to C#'s as operator.

# re: VB.NET Whidbey - The TryCast operator.

Wednesday, March 17, 2004 5:48 AM by Mike Schinkel
I agree!!! CType() is just weird. Better yet, how about a "CastAs" operator? i.e.

Dim x as String
y= 10 CastAs String

# re: VB.NET Whidbey - The TryCast operator.

Saturday, March 19, 2005 6:33 PM by aaron
I was looking for a list of the most common VB.net commands and functions to use as a reference sheet for coding, but haven't been able to find any online. I've got a few books but everything is spread out so much that its not much help for a quick reference. My email address is below with the @ changed to avoid spam.
Thanks in advance,
aaron

adrunkle(at)yahoo.com

# re: VB.NET Whidbey - The TryCast operator.

Tuesday, August 16, 2005 11:45 PM by Kerpal
VB.NET is becoming a clone of C# without the semi-colons! VB should have remained a hacky scripting style language and should never have been dragged into the .NET world.

# re: VB.NET Whidbey - The TryCast operator.

Monday, January 23, 2006 1:26 PM by Cory Smith
Kerpal, I think you've got your history confused. C# is the new kid on the block, so technically C# could be better described as being VB with all of the pinky-finger-gymnastics added to it so some developers could feel like they are playing in the same arena as other languages with simi-colons and curly-braces. (For the record, that's not my description... it's borrowed from a avid C++ developer friend of mine. Yes, I know, it's sometimes hard to believe that a C++ guy could be my friend.)

Besides, what's wrong with "hacky". Some of the best ideas come from hacking something together and refining from there. Where do you think this all came from anyway? It was people working out of their garage attempting to do things that had never been considered and, in a lot of cases, was considered "inappropriate" by the tie wearing engineers. ;-)

"Dragged into the .NET world" Funny... Why aren't you stating this about C++?

Go develop in your language of choice and give others the chance to do the same. If you really feel superior developing in C#, then by all means it's your right to do so. But it is *NOT* within your rights to demean those that choose not to do things the way you see them, especially since your view is extreme narrow minded and just plain wrong.
Anonymous comments are disabled