473,379 Members | 1,283 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,379 software developers and data experts.

Differences between these cast operations / statements?

i read this
http://msdn.microsoft.com/chats/tran...dn_csharp.aspx :

Cyrusn_MS (Expert):
Q: please explain the difference
1: Convert.ToInt32(o)
2: (int)o
3: o as int32
A: Rk: It all depends on what o's type is. Convert.ToInt32 will convert a
lot o standard framework types into an int32. For example, it will allow you
to convert 'true' into '1'. (int)o performs an explicit cast from the o
object to int, and will fail if that type doesn't have such a conversion
(like bool). o as Int32 is not allowed as you cannot have a value type on the
right side of an 'as'. THat's because 'as' returns null if it couldn't
perform the conversion and there is no null Int32 value.

maybe someone can explain this more in detail?

i have an additional point 4: int.Parse() whats about that?
thanks, toebens
Dec 14 '05 #1
6 1488
int.Parse accepts a string, not object. It tries to parse the string
into the int32 value, and will fail with FormatException if the string
is not a valid representation of an int.
The overloaded method Convert.ToInt32(string), call this method

Dec 14 '05 #2
Regarding (int)o, if it fails an exception will be thrown, whereas o as
int32 will return null if it 'fails' but will not thrown an exception.

Dec 14 '05 #3
As the original poster correctly stated *as* operator cannot be used in this
case (the case of converting to value types) because one of the possible
return values is *null*. Value types cannot be initialized with *null* Using
*as* in this case is compilation error.

toebens, which part of the conversion is not clear to you? As I can see you
got it correct.

Stoitcho Goutsev (100) [C# MVP]
"^MisterJingo^" <mi*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Regarding (int)o, if it fails an exception will be thrown, whereas o as
int32 will return null if it 'fails' but will not thrown an exception.

Dec 14 '05 #4
> toebens, which part of the conversion is not clear to you? As I can see you

so if i got it all then:
1: Convert.ToInt32 can be used for multiple datatypes
2: (int) only with an object - this object must be a "boxed" integer
3: o as int - cant be used because int cant be null - o as string would work
4: int.Parse() only works with a string as parameter

is that all right?

so i have to convert a ViewState["mySiteID"] with Convert.ToInt32 because it
ViewState returns an object!?? (i'm using this in asp.net)

Dec 14 '05 #5
> 2: (int) only with an object - this object must be a "boxed" integer

No, you can also say:

long aLong = 15;
int anInt = (int)aLong;


int x = 15;
int y = (int)x;

The latter is useless, but I believe it's allowed. The (int) cast
operator is used for two things: 1) To indicate to the compiler that
even though it doesn't know for sure that an object you've given it is
really an int, _you_ know, and it should generate code to unbox the
value into an int... if it's a boxed integer, as you pointed out, or 2)
To indicate an explicit coercion from one integer type to another.
so i have to convert a ViewState["mySiteID"] with Convert.ToInt32 because it
ViewState returns an object!?? (i'm using this in asp.net)

Well, that all depends. the ViewState indexer is _declared_ as
returning an object, but that means that it can return any object of
any type. So, what type is that object, really? That is, don't forget
the distinction between compile time and run time. At compile time, all
the compiler knows is that the ViewState indexer returns some kind of
object. At run time, however, that object will be of a specific type, a
type not predictable at compile time, but a specific type nonetheless.
So, what type is it?

If it's a boxed integer, or a boxed long, or... you can say

int id = (int)ViewState["MySiteID"];

If it's a string or something else, you'll need to say:

int id = Convert.ToInt32(ViewState["MySiteID"]);

If you have no idea what it will be, then you should use
Convert.ToInt32, as this will accept even an Int32 and "convert" it to
Int32, so it works in all valid cases.

The bottom line is that even though the compiler doesn't know what the
return type is, doesn't mean that _you_ don't know and can't instruct
the compiler appropriately.

Dec 14 '05 #6
Ah, I was in a rush and misread the question. Sorry about that.


Dec 14 '05 #7

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

Similar topics

by: Daniel | last post by:
Hi, Are there any differences between C# and VB.Net besides syntax? Performance-wise, how do they compare? Thanks, Dan
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
by: MS | last post by:
Just a general question here re VBA. Can anyone explain the differences between "!" and "." when refering to a control? eg Me!TxtBox and Me.TxtBox. What is difference between "+" and "&"...
by: hokieghal99 | last post by:
What is the difference between these two statements? Are there any major differences? const char filename = "ips_c.txt"; char filename = "ips_c.txt"; Thanks!!!
by: Alan Roberts | last post by:
Can someone please explain the following for me... I am trying to link to a .NET DLL from Excel. Excel needs to pass a reference to itself to the DLL and then the DLL needs to perform some work...
by: RdS | last post by:
Hello Would someone tell me the differences between following statements? When would you use one over the other? dim objtest1 as car dim objtest2 as new car dim objtest3 as car = new car ...
by: Scott M. | last post by:
Why will this fail: (short) txtQty.Text and this succeed? Convert.ToInt16(txtQty.Text)
by: blangela | last post by:
I am teaching a C programming course to C++ programmers and want to come up with list of _KEY_ differences between the two languages. Below is a list I have come up with so far. Have I missed any?...
by: xz | last post by:
I just wanna confirm my understanding about the differences between the signatures as follows, which are called all in the same way: void foo(Vertex a, Vertex b, Vertex c); void foo(Vertex & a,...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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$) { } ...
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...
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: 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 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.