473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Type conversion "as" in VB .NET

When converting a type to another using CType and if the type conversion
fails, it throw an exception. However, in C#, there is a keyword "as" which
only makes the variable Nothing (null) without throwing an exception. Is
there a plan at Microsoft VB .NET team to include a keyword equivalent to
"as"?
Nov 20 '05 #1
5 9633
I should hope not.

If I code a CType(...) and it fails then I damnned well want to know about
it.

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...
When converting a type to another using CType and if the type conversion
fails, it throw an exception. However, in C#, there is a keyword "as" which only makes the variable Nothing (null) without throwing an exception. Is
there a plan at Microsoft VB .NET team to include a keyword equivalent to
"as"?

Nov 20 '05 #2
Absolutely, you want to know about the exception, but there may be a
situation where you want to handle the failure of type conversion by
checking whether the variable is Nothing or not. If the variable is not
nothing, then the type conversion succeeded. Here is the example.

[VB .NET]
Dim MyObject As MyType

Try
MyObject = CType(SomeObject, MyType)
Catch
MyObject = Nothing
End Try

If Not MyObject Is Nothing Then
' Do Some process
End If

[C#]
MyType MyObject = SomeObject as MyType;

if (MyObject != null)
{
' Do some process
}
VB .NET code is significantly more. And I assume that since it uses
exception to handle situation like it, performance cost may be higher in VB
..NET than in C#...

"Stephany Young" <st******@sysoft.co.nz> wrote in message
news:uh**************@TK2MSFTNGP11.phx.gbl...
I should hope not.

If I code a CType(...) and it fails then I damnned well want to know about
it.

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...
When converting a type to another using CType and if the type conversion
fails, it throw an exception. However, in C#, there is a keyword "as"

which
only makes the variable Nothing (null) without throwing an exception. Is
there a plan at Microsoft VB .NET team to include a keyword equivalent to "as"?


Nov 20 '05 #3
Hi Stephany,

The 'as' keyword in C#, is just a shorthand. It doesn't necessarily hide
anything away from you. Assigning some object to a variable of a given type
when it is not that type doesn't fail. It successfully sets the variable to
null.

C#
object o = 24;

SomeSub (o as string); // passes null
string s = o as string; // s = null
VB
Dim o As Object
o = 24
Dim s As String
If o.GetType Is GetType(String) = False Then
s = Nothing
Else
s = o
End If
SomeSub (s) 'Needs the whole If statement first.

It's just VB windyness. That's all.

Regards,
Fergus
Nov 20 '05 #4
Yeah, yeah :-)

VB
Dim o As Object
o = 24
Dim s As String
If TypeOf o Is String Then
s = o
Else
s = Nothing
End If
SomeSub (s) 'Needs the whole If statement first.

Still windy.

Regards,
Fergus
Nov 20 '05 #5
Hayato,
When converting a type to another using CType and if the type conversion
fails, it throw an exception. BTW: You do know that using DirectCast is generally better than using CType
when casting reference types?
Is there a plan at Microsoft VB .NET team to include a keyword
equivalent to "as"?
Not that I am aware of. Some items identified for the next version of VB.NET
are documented at:

http://msdn.microsoft.com/vstudio/pr...o/roadmap.aspx

As Fergus stated, its a shorthand. A very handy shorthand. In addition to
CType & DirectCast I can see a use for "as".

You could use the Microsoft.Com Product Feedback system (MS Wish) to submit
a request that this be included:
http://register.microsoft.com/mswish...=EN-US&gssnb=1

Hope this helps
Jay

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl... When converting a type to another using CType and if the type conversion
fails, it throw an exception. However, in C#, there is a keyword "as" which only makes the variable Nothing (null) without throwing an exception. Is
there a plan at Microsoft VB .NET team to include a keyword equivalent to
"as"?

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
27
by: Curious Angel | last post by:
I have a resume in PDF format and I want anyone who LEFT-OR-RIGHT clicks the link to force the file to be saved, and in any event _not_ opened. Since the PDF will be in his cache in any event, I...
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
1
by: Alex | last post by:
Is that "as" returns null if failed to cast, doesn't throw an exception, so you can check the reference for null afterwards. straight casting will throw an InvalidCastException exception.
15
by: Bobby C. Jones | last post by:
Is there an advantage to either of these methods, or is it simply a matter of style? I used to use the "as" method, but later gave it up for the "is" method as the intent of the code seems a...
10
by: Max André Bündchen | last post by:
Hi, I must design a method that receive a Control object and return a single value if it is a TextBox object, another value if it is a ComboBox object and so on. If I try a "if-else-if" loop,...
5
by: Dave | last post by:
What is the benefit of using "as" vs the other? HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/"); vs. HttpWebRequest myReq...
1
by: Mark R. Dawson | last post by:
A while ago there was a post where someone asked how to find if a type was derived from another type, three ways were mentioned: Solution1:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.