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

(int) or convert.ToInt32 ?

Hey
I have following "problem".
int id = (int)SQlHelper.ExecuteScala(...) - it returns object ( which is int )
I get invalid cast.

Convert.ToInt32(...) works ok. Why ?
Jarod
Nov 17 '05 #1
8 14842
Hi,

because there si not implicit cast (int) on objects (actually it does not
know how to convert it if you don't tell how, that's why Convert.ToInt32
works. In this function there is a explicit conversion rule. Also there is
another way....

Beacause some return type implements IConvertible (intellisence doesnot
present it so you can check the msdn documentation) you can do this:

int MyValue = ((IConvertible)SQlHelper.ExecuteScalar(....)).ToIn t32(null);

actually is faster than Convert as stateted on "Maximizing .NET Performance
Addison-Wells 2002)

Cheers
Salva

--
Salvador Alvarez Patuel
Exony Ltd - London, UK
"Jarod" wrote:

Hey
I have following "problem".
int id = (int)SQlHelper.ExecuteScala(...) - it returns object ( which is int )
I get invalid cast.

Convert.ToInt32(...) works ok. Why ?
Jarod

Nov 17 '05 #2
>int id = (int)SQlHelper.ExecuteScala(...) - it returns object ( which is int )
I get invalid cast.

Convert.ToInt32(...) works ok. Why ?


Casting only works if the type is exactly int (which it probably isn't
in this case). Convert works for other types of boxed integers as well
(byte, short, ushort etc).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #3
Hi Jarod,
the equivalent of the null value in the db world for NET is System.DBNull
now from your scalar execution if the return is System.DBNull then the cast
will fail since System.DBNull is not a number(byFar...) but the
Convert.ToInt32 function if the value passed throws an exception in our case
it does, it return the integer 0, thus it didn't convert your value
actually.
Hope that helps.
"Jarod" <Ja***@discussions.microsoft.com> wrote in message
news:89**********************************@microsof t.com...
Hey
I have following "problem".
int id = (int)SQlHelper.ExecuteScala(...) - it returns object ( which is
int )
I get invalid cast.

Convert.ToInt32(...) works ok. Why ?
Jarod

Nov 17 '05 #4
Hey
Is t possible to convert object on int with "as" operator ? If yes how to ?
Jarod
Nov 17 '05 #5
hi

you can IF the object is a boxed int , or a type that can be implicitely
converted to int.

My advise is that you read a little about boxing and casting, it would make
you see it clearer , unfortunally I have no link in hand about these
concepts :(

you will have to google for them, or wait until somebody post a link

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jarod" <Ja***@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.com...
Hey
Is t possible to convert object on int with "as" operator ? If yes how to
?
Jarod

Nov 17 '05 #6
hi,

http://www.yoda.arachsys.com/csharp/faq/

you can find there several answers that deal with converting/casting
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #7
Jarod <Ja***@discussions.microsoft.com> wrote:
Is t possible to convert object on int with "as" operator ? If yes how to ?


No, "as" only applies to reference types.

From the C# language specification:

<quote>
In an operation of the form e as T, e must be an expression and T must
be a reference type.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
hi Jon,
Good aclaration :)
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jarod <Ja***@discussions.microsoft.com> wrote:
Is t possible to convert object on int with "as" operator ? If yes how to
?


No, "as" only applies to reference types.

From the C# language specification:

<quote>
In an operation of the form e as T, e must be an expression and T must
be a reference type.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #9

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

Similar topics

5
by: John Smith | last post by:
Hey folks, I know this is an old topic, but I can't find a definitive answer on google. How do I tell if an int has been initialized or not? I had been testing it like: if(myInt ==...
5
by: Asha | last post by:
greetings, i'm converting my value to int value using int32.parse() the problem is that, if its an empty value it returns me an error. what can i do to handle this error besides using a if...
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
5
by: Galahad | last post by:
I am using the code below to create a integer array. while(dr.Read()) { string StateID=dr.ToString(); int FacilityID=Convert.ToInt32(dr.ToString()); int FacilityIDs={FacilityID}; } ...
11
by: Code[R] | last post by:
hello all, i've a problem :s i've tried to convert a "system::string" (a textbox) to an "int" but i have only found a function to convert system::string to char and char to int :s : ...
17
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
2
by: tony | last post by:
Hello!! string number = "13"; int num; Is it exactly the same if I use num = Convert.ToInt32(number); OR num = int.Parse(number) Is it always in this case that I can choose whichever of...
4
by: tshad | last post by:
I am trying to convert a string character to an int where the string is all numbers. I tried: int test; string stemp = "5"; test = Convert.ToInt32(stemp); But test is equal to 53.
6
by: Tony | last post by:
Hello! It seems to me that both Int32.Parse(..) and Convert.ToInt32(...) static methods works in exactly the same way. Both can throw an exeption. So is it any different at all between these...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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...
0
isladogs
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 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.