VB.NET equivalent to C# "x y = z as y"

  • Thread starter Thread starter Jimi
  • Start date Start date
J

Jimi

I was wondering if there was a VB.NET equivalent to the following C#
statement:
SomeType SomeVar = SomeOtherVar as SomeType;

I know that in VB.NET you could have:
Dim SomeVar As SomeType = CType(SomeOtherVar, SomeType)

but this throws an exception if the cast fails while the C# example
above just sets SomeVar to null if the cast fails. (the C#
equivalent to this is "SomeType SomeVar = (SomeType)SomeOtherVar;".
 
* (e-mail address removed)-spam.invalid (Jimi) scripsit:
I was wondering if there was a VB.NET equivalent to the following C#
statement:
SomeType SomeVar = SomeOtherVar as SomeType;

I know that in VB.NET you could have:
Dim SomeVar As SomeType = CType(SomeOtherVar, SomeType)

but this throws an exception if the cast fails while the C# example
above just sets SomeVar to null if the cast fails. (the C#
equivalent to this is "SomeType SomeVar = (SomeType)SomeOtherVar;".

\\\
Dim SomeOtherVar As SomeType
If TypeOf SomeVar Is SomeType Then
SomeOtherVar = DirectCast(SomeVar, SomeType)
End If
If Not SomeOtherVar Is Nothing Then
...
End If
///

In VB.NET 2004/5:

\\\
Dim SimeOtherVar As SomeType = TryCast(SomeVar, SomeType)
If SomeOtherVar IsNot Nothing Then
...
End If
///
 
Thanks!
I was hoping for a one line version - but I see from the teaser you
included that that's not there until VS 2004/5 with "TryCast".
 
Herfried,
Did I miss something?

Dim SimeOtherVar As SomeType = TryCast(SomeVar, SomeType)

I don't remember seeing TryCast in the PDC release of Whidbey...

Jay
 
Jay B. Harlow said:
Did I miss something?
Dim SimeOtherVar As SomeType = TryCast(SomeVar, SomeType)

I don't remember seeing TryCast in the PDC release of Whidbey...

Jay: Part of the TryCast(), CatchCast(), FinallyCast() system :-)

Tom
 
Herfried,
I checked the PDC release of Whidbey, its not there!

The first sentence of the second paragraph, on the site you gave, I think
explains it:

<quote>In Whidbey, we're going to introduce YACO (Yet Another Conversion
Operator) called TryCast that's equivalent to the C# 'as' operator. </quote>

I'll have to add Paul's RSS to my RSS reader. I just finished his "The
Visual Basic .NET Programming Language" from Addison Wesley. Picked up a new
trick! You can use a Goto in a Catch block to go to the Try Block. (aka a
simple form of Retry for Try/Catch blocks).

Thanks for the heads up
Jay
 
Visual Basic .NET Programming Language" from Addison Wesley. Picked up a new
trick! You can use a Goto in a Catch block to go to the Try Block. (aka a
simple form of Retry for Try/Catch blocks).

Blasphemy!! Won't the programming gods smite you for using the G-keyword
in a program? :)

Do you put a label before the try or inside the try:

This:

TRYAGAIN:
Try
'blah blah
Catch
If somecondition then
goto TRYAGAIN
endif
Finally
End Try

Or this:

Try
TRYAGAIN:
'blah blah
Catch
If somecondition then
goto TRYAGAIN
endif
Finally
End Try
 
Jay,

* "Jay B. Harlow said:
I checked the PDC release of Whidbey, its not there!

I hope there will be a beta with more features soon.
The first sentence of the second paragraph, on the site you gave, I think
explains it:

<quote>In Whidbey, we're going to introduce YACO (Yet Another Conversion
Operator) called TryCast that's equivalent to the C# 'as' operator. </quote>

I like 'TryCast' much more than the very undescriptive 'as' (at least
non-native English speakers have problems to understand the purpose of
this operator when seeing it in source code).
I'll have to add Paul's RSS to my RSS reader. I just finished his "The
Visual Basic .NET Programming Language" from Addison Wesley. Picked up a new
trick! You can use a Goto in a Catch block to go to the Try Block. (aka a
simple form of Retry for Try/Catch blocks).

:->
 
Chris,
Blasphemy!! Won't the programming gods smite you for using the G-keyword
in a program? :)

Remember bad uses of Goto does not make Goto itself bad.
Do you put a label before the try or inside the try:

It needs to go Inside, I can see combining it with a MessageBox on specific
exceptions only (ones the user could recover from, such as a file format
errors after Open File dialog).

Try
TRYAGAIN:
'blah blah
Throw New System.IO.FileNotFoundException
Catch ex As System.IO.FileNotFoundException
If MessageBox.Show("What would you like to do?", "File not
found", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) = DialogResult.Retry Then
GoTo TRYAGAIN
Else
Throw ' let error propagate up
End If
End Try

Whether I rethrow the caught error to let it propagate up or not would
depend on where I was & what the "alternative" (to having the try fail) is.

Hope this helps
Jay
 
Herfried,
I like TryCast over As (especially considering As is already well defined in
VB.NET).

Not sure when we will see the next beta...

Jay
 
Instead of the two If, it could be better to use Try :

Dim SomeOtherVar As SomeType
Try
SomeOtherVar = CType(SomeVar, SomeType)
Catch ex As Exception
SomeOtherVar = Nothing
End Try
 
NM,
Remember If TypeOf will perform better then a Try/Catch.

Hence Herfried's example is the better way, in this case.

Hope this helps
Jay

NM said:
Instead of the two If, it could be better to use Try :

Dim SomeOtherVar As SomeType
Try
SomeOtherVar = CType(SomeVar, SomeType)
Catch ex As Exception
SomeOtherVar = Nothing
End Try
 
Ok thanks.


Jay B. Harlow said:
NM,
Remember If TypeOf will perform better then a Try/Catch.

Hence Herfried's example is the better way, in this case.

Hope this helps
Jay
 
Jay,
I like TryCast over As

TryCast is certainly better than As for VB. But I don't like the name
TryCast either. Probably because it reminds me of the MC++ operator
__try_cast which *does* throw InvalidCastException on failure, just
like DirectCast does today. Too confusing...

Plus I associate the word "Try" with SEH and exceptions, so it just
doesn't feel right.

I wish they had used some other name, such as SafeCast or
CastButNoExceptionsPlease. :-)



Mattias
 
Mattias,
Yea when I first read about TryCast, I was trying to think of a different
term, I like SafeCast.

Whatever is used it needs to be "short", CastButNoExceptionsPlease is a
little too long ;-))

Another option might be to stay with DirectCast and add an overloaded
parameter.

x = DirectCast(o, Type) ' causes exception
x = DirectCast(o, Type, True) ' causes exception
x = DirectCast(o, Type, False) ' exception free zone

But I don't know if I care for the above either, considering other
keywords/operators don't work that way.

I'm just glad we are getting the keyword/operator!

Jay
 
Back
Top