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
? 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
?
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
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
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
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
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
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
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 > > This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: David |
last post by:
what's the differences between:
int main(int argc,char* argv){
...
}
and:
int main(int argc,char** argv){
...
|
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...
|
by: August Karlstrom |
last post by:
Hello all,
If c is a char then is there any difference between
c = '\0'
and
c = 0
|
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;...
|
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...
|
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
|
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...
|
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>
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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: 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,...
|
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...
| |