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

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 1096
>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

0
by: Pedro Werneck | last post by:
Hi list I'm trying to implement a new type in a C extension and it must support some binary operators, like &, |, ^, << and >>. With &, | and ^, the method must receive another object of the...
20
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
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,...

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.