473,569 Members | 2,761 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

casting an object to a type

Can someone explain how to cast an object to a specific type during runtime?

// This line of code tells me the objects type.
System.Type type = System.Type.Get Type("string of the type");

// This line of code compares the objects, but the compiler doesn't like my
casting.
return ((type)object1 <= (type)object2) ? true : false;

The compiler doesn't like my casting of (type)object1 or (type)object2.

Steve
Nov 17 '05 #1
3 1664
Steve Teeples <St**********@d iscussions.micr osoft.com> wrote:
Can someone explain how to cast an object to a specific type during
runtime?
Casts are there for the compiler, really - they don't make sense to
perform at runtime.
// This line of code tells me the objects type.
System.Type type = System.Type.Get Type("string of the type");

// This line of code compares the objects, but the compiler doesn't like my
casting.
return ((type)object1 <= (type)object2) ? true : false;

The compiler doesn't like my casting of (type)object1 or (type)object2.


No, it wouldn't. If your objects definitely implement IComparable, you
should cast both sides to that and call Compare - that should give the
same effect.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
All my objects will be base numeric types (e.g., Int32, UInt64, etc.) . How
do I use IComparable? I've never used it before?

"Jon Skeet [C# MVP]" wrote:
Steve Teeples <St**********@d iscussions.micr osoft.com> wrote:
Can someone explain how to cast an object to a specific type during
runtime?


Casts are there for the compiler, really - they don't make sense to
perform at runtime.
// This line of code tells me the objects type.
System.Type type = System.Type.Get Type("string of the type");

// This line of code compares the objects, but the compiler doesn't like my
casting.
return ((type)object1 <= (type)object2) ? true : false;

The compiler doesn't like my casting of (type)object1 or (type)object2.


No, it wouldn't. If your objects definitely implement IComparable, you
should cast both sides to that and call Compare - that should give the
same effect.

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

Nov 17 '05 #3
I've played with it and figured it out. Thanks Jon for your help at such a
late hour.

"Jon Skeet [C# MVP]" wrote:
Steve Teeples <St**********@d iscussions.micr osoft.com> wrote:
Can someone explain how to cast an object to a specific type during
runtime?


Casts are there for the compiler, really - they don't make sense to
perform at runtime.
// This line of code tells me the objects type.
System.Type type = System.Type.Get Type("string of the type");

// This line of code compares the objects, but the compiler doesn't like my
casting.
return ((type)object1 <= (type)object2) ? true : false;

The compiler doesn't like my casting of (type)object1 or (type)object2.


No, it wouldn't. If your objects definitely implement IComparable, you
should cast both sides to that and call Compare - that should give the
same effect.

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

Nov 17 '05 #4

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

Similar topics

4
3449
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 good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor,...
19
1734
by: Ramesh Tharma | last post by:
Hi, Is any one knows what's wrong with the following code, I was told that it will compile and run but it will crash for some values. Assume that variables are initilized. char* c; long* lg;
7
3654
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; but it complains for the following lines
7
30765
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 along. It says it's an invalid cast. However, if I use the Convert.ToInt32() method (for example) things will work. At least, that's how it appears to...
18
2158
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
61
4524
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 perform different manipulations on it. For example, I have a complex data structure, which I can represent in a VB6 TYPE declaration, but I cannot...
23
3490
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 the Inherit statement).
3
1639
by: JimM | last post by:
I am trying to create a method in VS 2003 that validates an object argument is of the proper type and within a range of values. I am trying to use a Type to define the casting and object type for the validation. But I get and error of "The type or namespace name 't' could not be found (are you missing a using directive or and assembly...
7
13641
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; }
7
2641
by: William S Fulton | last post by:
I'm looking for the name of the following casting style in order to do some reading around on it and why it is sometimes used. unsigned long long ull = 0; void * ptr = 0; ull = *(unsigned long long*)&ptr; As opposed to the more usual casting:
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.