473,226 Members | 1,359 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,226 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 1172
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>
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.