473,387 Members | 1,365 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,387 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 2361
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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 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.