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

casting from object

I am having problem casting from object. I want to pass in an
value type as an object and then store it as a particular type.
The cast from object throws an exception if the types do not match.
class test {
short x;

// o can be any of the value types int, long, float
public void Set( object o ) {
x = ( short ) o; /// thows an exception if o is an int
}
public void Set2( object o ) {
int i = ( int ) o;
x = ( short ) i; /// works
}

}

....
test t = new test();
int j = 42;
t.Set2( j ); // good
t.Set( j ); // bad

Why wont the cast in Set work?
What good is object if I need to know what freaking type it is to use it?
Is there a stronger cast?

I suppose I could write a long if-else-if that test all the possible types
of the object o and cast it to the input o type and then cast that to
short but that is pretty lame.

Any ideas?
Also I am not allowed to use generics. Sigh....

Karl
Nov 15 '05 #1
2 27646
Try Convert.ChangeType

Jonathan Schafer

On 28 Jul 2003 07:57:57 -0700, me********@yahoo.com (Karl Meissner)
wrote:
I am having problem casting from object. I want to pass in an
value type as an object and then store it as a particular type.
The cast from object throws an exception if the types do not match.
class test {
short x;

// o can be any of the value types int, long, float
public void Set( object o ) {
x = ( short ) o; /// thows an exception if o is an int
}
public void Set2( object o ) {
int i = ( int ) o;
x = ( short ) i; /// works
}

}

...
test t = new test();
int j = 42;
t.Set2( j ); // good
t.Set( j ); // bad

Why wont the cast in Set work?
What good is object if I need to know what freaking type it is to use it?
Is there a stronger cast?

I suppose I could write a long if-else-if that test all the possible types
of the object o and cast it to the input o type and then cast that to
short but that is pretty lame.

Any ideas?
Also I am not allowed to use generics. Sigh....

Karl


Nov 15 '05 #2
If I had to guess, I'd say it's because of some pre-cast bounds or
address or address-size checking or something along those lines.

Personally I'd use a cascading thing like you're doing in Set2 anyway,
start with a double -> int -> short, as an example.

At least that way, you're always guaranteed to get a return value, if
not boxed up in the type you're expecting, or you'd be able to stack
trace the failures and log your problems.

me********@yahoo.com (Karl Meissner) wrote in message news:<46**************************@posting.google. com>...
I am having problem casting from object. I want to pass in an
value type as an object and then store it as a particular type.
The cast from object throws an exception if the types do not match.
class test {
short x;

// o can be any of the value types int, long, float
public void Set( object o ) {
x = ( short ) o; /// thows an exception if o is an int
}
public void Set2( object o ) {
int i = ( int ) o;
x = ( short ) i; /// works
}

}

...
test t = new test();
int j = 42;
t.Set2( j ); // good
t.Set( j ); // bad

Why wont the cast in Set work?
What good is object if I need to know what freaking type it is to use it?
Is there a stronger cast?

I suppose I could write a long if-else-if that test all the possible types
of the object o and cast it to the input o type and then cast that to
short but that is pretty lame.

Any ideas?
Also I am not allowed to use generics. Sigh....

Karl

Nov 15 '05 #3

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
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
7
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;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
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; }
1
by: madumm | last post by:
Hi all I really need a solution for the following:: ------------------------------------------------------------------------------------- Say i have a font object called 'myFont' as shown below;...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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)...
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.