473,326 Members | 2,147 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.

casting problem

in the following code:

object obj = this.ViewState["currentCreditLimit"];
string s = obj.GetType().ToString();
currentCreditLimit = (double) this.ViewState["currentCreditLimit"];

ViewState is a StateBag object (dictionary type object)
the String s shows me that the type is System.Int32, the value contained is
0 ( I can see it in the debugger)

I understand well that a double can contain bigger values than what contain
an Int32 but I do not understand why I got a casting exception. As I think
that you can cast any number types into any others. The only risk being to
overflow the capacity of the target type. Which is not the case here as my
value is 0.

Also, if I do something like the following it will run fine.
int i = 0;
double d = 0;
d = i;

Could someone explain me what is going on?

Thanks

Francois


Nov 15 '05 #1
5 2356
Francois Malgreve wrote:
in the following code:

object obj = this.ViewState["currentCreditLimit"];
string s = obj.GetType().ToString();
currentCreditLimit = (double) this.ViewState["currentCreditLimit"];

ViewState is a StateBag object (dictionary type object)
the String s shows me that the type is System.Int32, the value contained is
0 ( I can see it in the debugger)

I understand well that a double can contain bigger values than what contain
an Int32 but I do not understand why I got a casting exception. As I think
that you can cast any number types into any others. The only risk being to
overflow the capacity of the target type. Which is not the case here as my
value is 0.

Also, if I do something like the following it will run fine.
int i = 0;
double d = 0;
d = i;

Could someone explain me what is going on?

Thanks

Francois

The problem is with boxing. See, an int has to boxed to be treated as
an object. In C#, when you unbox a boxed type, you have to cast it to
the original boxed type *first*, otherwise you will get a
InvalidCastException.

Use

currentCreditLimit = (double)(int) this.ViewState["currentCreditLimit"];

Nov 15 '05 #2
Thank you,
I always thought that one advantage of C# is that boxing and unboxing is
automatic. Then what I understand from what you say is that boxing and
unboxing is automatic except in the case I cast a value to an unboxed type
different that the native type? Which would mean I need to unbox manually :

(int) this.ViewState["currentCreditLimit"];
before being able to cast :
(double)(int) this.ViewState["currentCreditLimit"];

Or do you mean that in C# I always need to unbox manually ? That the
unboxing needs to be written in the code but that only the boxing will be
implicit?

The last thing i am not sure about:
I think that the boxed type is a reference type wrapped in an object
(typically System.Object) and the unboxed type is the value type like
System.Int32 (keyword int in c#), right?

Thanks again for your explanations,

Best regards,
Francois
"Tyler Dixon" <td****@telus.net> wrote in message
news:cgjwb.2358$oN2.2048@edtnps84...
Francois Malgreve wrote:
in the following code:

object obj = this.ViewState["currentCreditLimit"];
string s = obj.GetType().ToString();
currentCreditLimit = (double) this.ViewState["currentCreditLimit"];

ViewState is a StateBag object (dictionary type object)
the String s shows me that the type is System.Int32, the value contained is 0 ( I can see it in the debugger)

I understand well that a double can contain bigger values than what contain an Int32 but I do not understand why I got a casting exception. As I think that you can cast any number types into any others. The only risk being to overflow the capacity of the target type. Which is not the case here as my value is 0.

Also, if I do something like the following it will run fine.
int i = 0;
double d = 0;
d = i;

Could someone explain me what is going on?

Thanks

Francois

The problem is with boxing. See, an int has to boxed to be treated as
an object. In C#, when you unbox a boxed type, you have to cast it to
the original boxed type *first*, otherwise you will get a
InvalidCastException.

Use

currentCreditLimit = (double)(int) this.ViewState["currentCreditLimit"];

Nov 15 '05 #3
Thank you,
I always thought that one advantage of C# is that boxing and unboxing is
automatic. Then what I understand from what you say is that boxing and
unboxing is automatic except in the case I cast a value to an unboxed type
different that the native type? Which would mean I need to unbox manually :

(int) this.ViewState["currentCreditLimit"];
before being able to cast :
(double)(int) this.ViewState["currentCreditLimit"];

Or do you mean that in C# I always need to unbox manually ? That the
unboxing needs to be written in the code but that only the boxing will be
implicit?

The last thing i am not sure about:
I think that the boxed type is a reference type wrapped in an object
(typically System.Object) and the unboxed type is the value type like
System.Int32 (keyword int in c#), right?

Thanks again for your explanations,

Best regards,
Francois
"Tyler Dixon" <td****@telus.net> wrote in message
news:cgjwb.2358$oN2.2048@edtnps84...
Francois Malgreve wrote:
in the following code:

object obj = this.ViewState["currentCreditLimit"];
string s = obj.GetType().ToString();
currentCreditLimit = (double) this.ViewState["currentCreditLimit"];

ViewState is a StateBag object (dictionary type object)
the String s shows me that the type is System.Int32, the value contained is 0 ( I can see it in the debugger)

I understand well that a double can contain bigger values than what contain an Int32 but I do not understand why I got a casting exception. As I think that you can cast any number types into any others. The only risk being to overflow the capacity of the target type. Which is not the case here as my value is 0.

Also, if I do something like the following it will run fine.
int i = 0;
double d = 0;
d = i;

Could someone explain me what is going on?

Thanks

Francois

The problem is with boxing. See, an int has to boxed to be treated as
an object. In C#, when you unbox a boxed type, you have to cast it to
the original boxed type *first*, otherwise you will get a
InvalidCastException.

Use

currentCreditLimit = (double)(int) this.ViewState["currentCreditLimit"];

Nov 15 '05 #4
<"Francois Malgreve" <francois at bettinghouses.com>> wrote:
Thank you,
I always thought that one advantage of C# is that boxing and unboxing is
automatic. Then what I understand from what you say is that boxing and
unboxing is automatic except in the case I cast a value to an unboxed type
different that the native type? Which would mean I need to unbox manually :

(int) this.ViewState["currentCreditLimit"];
before being able to cast :
(double)(int) this.ViewState["currentCreditLimit"];

Or do you mean that in C# I always need to unbox manually ? That the
unboxing needs to be written in the code but that only the boxing will be
implicit?
Well, unboxing is implicit in that it's done with a normal cast rather
than you having to specify an unboxing operation, but yes, other than
that it's explicit. This is equivalent to having to cast for reference
types, too, eg

string x = (string) myHashTable["hello"];
The last thing i am not sure about:
I think that the boxed type is a reference type wrapped in an object
(typically System.Object) and the unboxed type is the value type like
System.Int32 (keyword int in c#), right?


Yes - apart from the "typically System.Object" - I believe the type of
the wrapper object is distinct from System.Object, and indeed I would
think it would *have* to be in order to tell the difference between a
plain object and a wrapped value. (And to store the data, of course.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
The root of the problem is that you're not placing the proper value in to
begin with

Whatever setting ViewState["currentCreditLimit"] is setting it to an INTEGER
value. If your intention is to store a DECIMAL value then you need to be
consistent by initializing it with a DECIMAL value either explicitly (
ViewState["currentCreditLimit"]=new Decimal(0) or via the "d" specifier,
ViewState["currentCreditLimit"] = 0d

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Tyler Dixon" <td****@telus.net> wrote in message
news:cgjwb.2358$oN2.2048@edtnps84...
Francois Malgreve wrote:
in the following code:

object obj = this.ViewState["currentCreditLimit"];
string s = obj.GetType().ToString();
currentCreditLimit = (double) this.ViewState["currentCreditLimit"];

ViewState is a StateBag object (dictionary type object)
the String s shows me that the type is System.Int32, the value contained is 0 ( I can see it in the debugger)

I understand well that a double can contain bigger values than what contain an Int32 but I do not understand why I got a casting exception. As I think that you can cast any number types into any others. The only risk being to overflow the capacity of the target type. Which is not the case here as my value is 0.

Also, if I do something like the following it will run fine.
int i = 0;
double d = 0;
d = i;

Could someone explain me what is going on?

Thanks

Francois

The problem is with boxing. See, an int has to boxed to be treated as
an object. In C#, when you unbox a boxed type, you have to cast it to
the original boxed type *first*, otherwise you will get a
InvalidCastException.

Use

currentCreditLimit = (double)(int) this.ViewState["currentCreditLimit"];

Nov 15 '05 #6

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

Similar topics

5
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
11
by: Vinod | last post by:
Hi, I am working in the project where VC6 code is ported to VC8 (VC++ .Net 2005) I got a problem when I cast a double value to unsigned int. Problem is I couldn’t get the proper value after...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: 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: 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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.