473,408 Members | 2,477 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,408 software developers and data experts.

Difference between type castings?

I'm starting to see a lot of
"somevar as sometype"

Just cause i'm curious, what's the difference between that and

(sometype) somevar

?
Nov 29 '05 #1
8 1179
The "as" operator is generally a safer way of performing type conversion.
From the help...

"The as operator is like a cast except that it yields null on conversion
failure instead of raising an exception."

http://msdn.microsoft.com/library/de...ml/vclrfas.asp

--
Tim Wilson
..NET Compact Framework MVP

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:eL****************@tk2msftngp13.phx.gbl...
I'm starting to see a lot of
"somevar as sometype"

Just cause i'm curious, what's the difference between that and

(sometype) somevar

?

Nov 29 '05 #2
Hi Benny,

x = somevar as sometype
is equivalent to:
if somevar is sometype
x = (sometype) somevar;
else
x = null;

Thus, "somevar as sometype" never raises an InvalidCastException. If
the cast is illegal, null is returned instead.

Thi - http://thith.blogspot.com

Nov 29 '05 #3
Hi,

I would also like to add, that the "as" casting is not supported on
value types. For value types, you must always use the (cast) casting.

-Lenard

Truong Hong Thi wrote:
Hi Benny,

x = somevar as sometype
is equivalent to:
if somevar is sometype
x = (sometype) somevar;
else
x = null;

Thus, "somevar as sometype" never raises an InvalidCastException. If
the cast is illegal, null is returned instead.

Thi - http://thith.blogspot.com

Nov 29 '05 #4
Hi Tim,

I'm not taking issue with you here, just wanted to get more info.

You say that "as" is generally safer. I'm thinking its not as you now
don't know if the cast is invalid. Only safer if you don't need to know??

Thoughts?

Simon

Tim Wilson wrote:
The "as" operator is generally a safer way of performing type conversion.
From the help...

"The as operator is like a cast except that it yields null on conversion
failure instead of raising an exception."

http://msdn.microsoft.com/library/de...ml/vclrfas.asp

Nov 29 '05 #5
Sure you do know if it is invalid. If returns a null reference if it is
not valid.

When you are getting out objects from a non-typed DataRow for example or
other typeless container, it is easier to use "as" than to put every
member access into a try block. Of course you can do that - I guess it
is also personal preference, which one you use.

-Lenard
Simon wrote:
Hi Tim,

I'm not taking issue with you here, just wanted to get more info.

You say that "as" is generally safer. I'm thinking its not as you now
don't know if the cast is invalid. Only safer if you don't need to know??

Thoughts?

Simon

Tim Wilson wrote:
The "as" operator is generally a safer way of performing type conversion.
From the help...

"The as operator is like a cast except that it yields null on conversion
failure instead of raising an exception."

http://msdn.microsoft.com/library/de...ml/vclrfas.asp

Nov 29 '05 #6
Sorry, I shoudl have explained better. What if NULL IS the value. Do you
see what I mean?

Lenard Gunda wrote:
Sure you do know if it is invalid. If returns a null reference if it is
not valid.

When you are getting out objects from a non-typed DataRow for example or
other typeless container, it is easier to use "as" than to put every
member access into a try block. Of course you can do that - I guess it
is also personal preference, which one you use.

-Lenard
Simon wrote:
Hi Tim,

I'm not taking issue with you here, just wanted to get more info.

You say that "as" is generally safer. I'm thinking its not as you now
don't know if the cast is invalid. Only safer if you don't need to know??

Thoughts?

Simon

Tim Wilson wrote:
The "as" operator is generally a safer way of performing type
conversion.
From the help...

"The as operator is like a cast except that it yields null on conversion
failure instead of raising an exception."

http://msdn.microsoft.com/library/de...ml/vclrfas.asp

Nov 29 '05 #7
Hi,

If you have a null value, you can cast that to anything you like. If
will not be an error. I don't think this would be a problem.

-Lenard

Simon wrote:
Sorry, I shoudl have explained better. What if NULL IS the value. Do you
see what I mean?

Lenard Gunda wrote:
Sure you do know if it is invalid. If returns a null reference if it
is not valid.

When you are getting out objects from a non-typed DataRow for example
or other typeless container, it is easier to use "as" than to put
every member access into a try block. Of course you can do that - I
guess it is also personal preference, which one you use.

-Lenard
Simon wrote:
Hi Tim,

I'm not taking issue with you here, just wanted to get more info.

You say that "as" is generally safer. I'm thinking its not as you now
don't know if the cast is invalid. Only safer if you don't need to
know??

Thoughts?

Simon

Tim Wilson wrote:

The "as" operator is generally a safer way of performing type
conversion.
From the help...

"The as operator is like a cast except that it yields null on
conversion
failure instead of raising an exception."

http://msdn.microsoft.com/library/de...ml/vclrfas.asp

Nov 29 '05 #8
Sorry I'll engage the brain first next time. Yup I get ya.

Cheers

Simon

Lenard Gunda wrote:
Hi,

If you have a null value, you can cast that to anything you like. If
will not be an error. I don't think this would be a problem.

-Lenard

Simon wrote:
Sorry, I shoudl have explained better. What if NULL IS the value. Do
you see what I mean?

Lenard Gunda wrote:
Sure you do know if it is invalid. If returns a null reference if it
is not valid.

When you are getting out objects from a non-typed DataRow for example
or other typeless container, it is easier to use "as" than to put
every member access into a try block. Of course you can do that - I
guess it is also personal preference, which one you use.

-Lenard
Simon wrote:

Hi Tim,

I'm not taking issue with you here, just wanted to get more info.

You say that "as" is generally safer. I'm thinking its not as you
now don't know if the cast is invalid. Only safer if you don't need
to know??

Thoughts?

Simon

Tim Wilson wrote:

> The "as" operator is generally a safer way of performing type
> conversion.
> From the help...
>
> "The as operator is like a cast except that it yields null on
> conversion
> failure instead of raising an exception."
>
> http://msdn.microsoft.com/library/de...ml/vclrfas.asp
>
>

Nov 29 '05 #9

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

Similar topics

0
by: Arne Claus | last post by:
I've got a tricky problem here. I try to create an object-hierarchie, based on a template base class like template <class T> class Node { addChild(Node<T> *) =0; // the Node-Container wil be...
10
by: David | last post by:
what's the differences between: int main(int argc,char* argv){ ... } and: int main(int argc,char** argv){ ...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
39
by: August Karlstrom | last post by:
Hello all, If c is a char then is there any difference between c = '\0' and c = 0
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
6
by: mthread | last post by:
Hi, I am learning C++ and I have been told that an object can be created either by using calloc or new. If it is true, can some one tell me what is the difference b/w using these two function...
10
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
21
by: Nikolaus Rath | last post by:
Hello, Can someone explain to me the difference between a type and a class? After reading http://www.cafepy.com/article/python_types_and_objects/ it seems to me that classes and types are...
9
by: viki1967 | last post by:
Hi all! This new forum its great! :) Congratulations !!! My answer: why this my code not working? Nothing error but not work the difference.... : <html>
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.