473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can I < or > with two objects of any type passed as object?

Hi,
I'm doing comparisons over arguments passed as Object... they can be of any
type, but must be comparable using < or >... unfortunatly, the compiler
does not give the possibility to compare 2 Object instances with < or >...
How can I do this without knowing the type of the objects?

Here's an example :

private void someMethod()
{
int m_X = 5;
float m_Y = 10.7f;

if (CompareGreaterThan(m_X, m_Y))
// DO STUFF
}

private bool CompareGreaterThan(object value1, object value2)
{
return (value1 value2); // Here I can't do
}

thanks

ThunderMusic
Aug 30 '06 #1
9 1026
>I'm doing comparisons over arguments passed as Object... they can be of any
type, but must be comparable using < or >...
Why not use the IComparable interface instead?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 30 '06 #2
good idea but it does not work as "expected"...

int x = 5;
decimal y = 6;
object obj = x;
object obj2 = y;
if (obj is IComparable)
MessageBox.Show(((bool)(((IComparable)obj).Compare To(obj2) >
0)).ToString());

I get an ArgumentException on the last line saying the argument must be a
System.Int32 even if an overload has an Object argument.... but doing this
works fine :
int x = 5;
decimal y = 6;
MessageBox.Show(((bool)(x y)).ToString());
thanks for trying... do you have another solution?

ThunderMusic

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:u0*************@TK2MSFTNGP06.phx.gbl...
>
>>I'm doing comparisons over arguments passed as Object... they can be of
any
type, but must be comparable using < or >...

Why not use the IComparable interface instead?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Aug 30 '06 #3
*"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
* Hi,
* I'm doing comparisons over arguments passed as Object... they can be of
any
* type, but must be comparable using < or >... unfortunatly, the compiler
* does not give the possibility to compare 2 Object instances with < or >...
* How can I do this without knowing the type of the objects?
*
* Here's an example :
*
* private void someMethod()
* {
* int m_X = 5;
* float m_Y = 10.7f;
*
* if (CompareGreaterThan(m_X, m_Y))
* // DO STUFF
* }
*
* private bool CompareGreaterThan(object value1, object value2)
* {
* return (value1 value2); // Here I can't do
* }
*
* thanks
*
* ThunderMusic
*
*

ThunderMusic,

Is this an instance where generics where help out?
For example:

private void SomeMethod()
{
int x = 5;
float y = 10.7f;

CompareGreaterThan<float>(x,y);
}

private static bool CompareGreaterThan <T(T value1, T value2)
{
return (((ICompareable)value1).CompareTo(((Compareable)va lue2) 0);
}
This worked for me.

-MH

Aug 30 '06 #4
The problem is that you can't use the and < operators, because
overload selection is done at compile time, and you don't know the
types until runtime. So, operator overloading, etc, is out of the
question.

However, type coercion code is inserted at compile time, so the
convenient feature of being able to coerce int x to a decimal in order
to compare with y isn't available when you know the two types.

So, there is no out-of-the-box solution to this problem. You must write
your own run-time type checking and call the appropriate comparison
method (in the case of built-in types) and/or do smart things in the
CompareTo(object o) method for your own types.

ThunderMusic wrote:
good idea but it does not work as "expected"...

int x = 5;
decimal y = 6;
object obj = x;
object obj2 = y;
if (obj is IComparable)
MessageBox.Show(((bool)(((IComparable)obj).Compare To(obj2) >
0)).ToString());

I get an ArgumentException on the last line saying the argument must be a
System.Int32 even if an overload has an Object argument.... but doing this
works fine :
int x = 5;
decimal y = 6;
MessageBox.Show(((bool)(x y)).ToString());
thanks for trying... do you have another solution?

ThunderMusic

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:u0*************@TK2MSFTNGP06.phx.gbl...
>I'm doing comparisons over arguments passed as Object... they can be of
any
type, but must be comparable using < or >...
Why not use the IComparable interface instead?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 30 '06 #5
In your case, you are comparing random objects, which makes things
difficult. If you know you can always cast up to a supertype (for example,
they will never overflow a single, or a double), you can cast things up and
compare. This wll work for numerics only, of course, but is an option. You
will have to perform the testing (to ensure one is not a string, for
example) and cast up.

I am not sure I have ever seen a system where random garbage needed to be
compared. It is also rare to have a properly designed system where
everything is seen as an object. Sure, it creates a very generic and loosely
coupled system, but the cost for a completely generic system can be rather
large.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hi,
I'm doing comparisons over arguments passed as Object... they can be of
any type, but must be comparable using < or >... unfortunatly, the
compiler does not give the possibility to compare 2 Object instances with
< or >... How can I do this without knowing the type of the objects?

Here's an example :

private void someMethod()
{
int m_X = 5;
float m_Y = 10.7f;

if (CompareGreaterThan(m_X, m_Y))
// DO STUFF
}

private bool CompareGreaterThan(object value1, object value2)
{
return (value1 value2); // Here I can't do
}

thanks

ThunderMusic

Aug 31 '06 #6
Actually, we will be comparing values that come from a database to values we
provide in the code (no user interaction and classes, only value types).. So
it will mainly be byte, int, decimal and string.... but I wanted to avoid
casting my types every time a comparison is done... but if I can't do
without casting, I will cast...

thanks to everyone... if you have any other ideas, please share... ;)

ThunderMusic
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamMwrote in
message news:ub**************@TK2MSFTNGP02.phx.gbl...
In your case, you are comparing random objects, which makes things
difficult. If you know you can always cast up to a supertype (for example,
they will never overflow a single, or a double), you can cast things up
and compare. This wll work for numerics only, of course, but is an option.
You will have to perform the testing (to ensure one is not a string, for
example) and cast up.

I am not sure I have ever seen a system where random garbage needed to be
compared. It is also rare to have a properly designed system where
everything is seen as an object. Sure, it creates a very generic and
loosely coupled system, but the cost for a completely generic system can
be rather large.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>Hi,
I'm doing comparisons over arguments passed as Object... they can be of
any type, but must be comparable using < or >... unfortunatly, the
compiler does not give the possibility to compare 2 Object instances with
< or >... How can I do this without knowing the type of the objects?

Here's an example :

private void someMethod()
{
int m_X = 5;
float m_Y = 10.7f;

if (CompareGreaterThan(m_X, m_Y))
// DO STUFF
}

private bool CompareGreaterThan(object value1, object value2)
{
return (value1 value2); // Here I can't do
}

thanks

ThunderMusic


Aug 31 '06 #7
Great Idea... I will think about it so I can be sure it can be applied
correctly in my case and I'll probably use this way... ;)

thanks a lot...

ThunderMusic

"Michael" <mh***@domain.comwrote in message
news:OV**************@TK2MSFTNGP05.phx.gbl...
*"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
* Hi,
* I'm doing comparisons over arguments passed as Object... they can be of
any
* type, but must be comparable using < or >... unfortunatly, the compiler
* does not give the possibility to compare 2 Object instances with < or
>...
* How can I do this without knowing the type of the objects?
*
* Here's an example :
*
* private void someMethod()
* {
* int m_X = 5;
* float m_Y = 10.7f;
*
* if (CompareGreaterThan(m_X, m_Y))
* // DO STUFF
* }
*
* private bool CompareGreaterThan(object value1, object value2)
* {
* return (value1 value2); // Here I can't do
* }
*
* thanks
*
* ThunderMusic
*
*

ThunderMusic,

Is this an instance where generics where help out?
For example:

private void SomeMethod()
{
int x = 5;
float y = 10.7f;

CompareGreaterThan<float>(x,y);
}

private static bool CompareGreaterThan <T(T value1, T value2)
{
return (((ICompareable)value1).CompareTo(((Compareable)va lue2) 0);
}
This worked for me.

-MH

Aug 31 '06 #8

How can I do this without knowing the type of the objects?
You cant, but did you ever consider that you should be using stronger
typing?

So youre using values of a database, and youre pulling them out as
Objects, and comparing them to other things declared as Objects in your
code.. this is nonsense, unless you have declared every database column
as RAW..

If you have a string column, then why not use a strongly typed DataSet
that defines that column to the client as a string.. then you can jsut
say:

if(myDataRow.SomeStringColumn "some literal in my code")
..NET is strongly typed for a reason; dont strip it away

Aug 31 '06 #9
actually it's because we will try to use the same set of methods for all our
comparison needs for an access control mecanism... so anything could be
compared to anything, but it will always (or almost) be value types except
in the case of isNull, isNotNull or DateTime... so that why Object was a
must, but I'll go with the generics idea, it works great...

thanks

ThunderMusic

"cjard" <mc**@aber.ac.ukwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>
>How can I do this without knowing the type of the objects?

You cant, but did you ever consider that you should be using stronger
typing?

So youre using values of a database, and youre pulling them out as
Objects, and comparing them to other things declared as Objects in your
code.. this is nonsense, unless you have declared every database column
as RAW..

If you have a string column, then why not use a strongly typed DataSet
that defines that column to the client as a string.. then you can jsut
say:

if(myDataRow.SomeStringColumn "some literal in my code")
.NET is strongly typed for a reason; dont strip it away

Aug 31 '06 #10

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

Similar topics

20
2162
by: Oliver S. | last post by:
I was often annoyed about the performance of std::basic-string because of complex memory-allocators working in the background to found the work of std::allocator<T>. Basically, the performance...
8
2221
by: ASP Yaboh | last post by:
I have an ArrayList of data gathered from a database. I want to create a web page from this data by creating a <table>, each cell in each row displays the appropriate data. One of those cells in...
5
21662
by: Jimp | last post by:
Why can't I cast List<MyObject> to ICollection<IMyObject>. MyObject implements IMyObject, and of course, List implements ICollection. Thanks
9
1097
by: ThunderMusic | last post by:
Hi, I'm doing comparisons over arguments passed as Object... they can be of any type, but must be comparable using < or >... unfortunatly, the compiler does not give the possibility to compare 2...
8
6231
by: =?Utf-8?B?V2hpdG5leSBLZXc=?= | last post by:
Hi there, I'm having a bit of trouble using an HRASCONN object as a class member variable inside my managed C++ class. When I call RasDial() and pass in the address of my HRASCONN object, I get...
22
3458
by: amygdala | last post by:
Hi, I'm trying to grasp OOP to build an interface using class objects that lets me access database tables easily. You have probably seen this before, or maybe even built it yourself at some...
5
2371
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
5
3822
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
44
39122
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
0
7125
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
7002
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
5462
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,...
1
4908
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...
0
4588
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...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
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 ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
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...

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.