473,414 Members | 1,609 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,414 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 27651
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.