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

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.GetType("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 1645
Steve Teeples <St**********@discussions.microsoft.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.GetType("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.com>
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**********@discussions.microsoft.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.GetType("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.com>
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**********@discussions.microsoft.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.GetType("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.com>
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
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...
19
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*...
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...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
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...
3
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...
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; }
7
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...
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: 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...
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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.