473,387 Members | 1,379 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,387 software developers and data experts.

compare two object

How can I understand if two object obj1 and obj2 have same type?

Can I do: if (obj1.GetType()==obj2.GetType()) return true;?

Moto u1;
Moto u2;

I call Compare(u1,u2,">")

class Moto have override operator >.

How can I use reflection in Compare fuction to call operator > of Moto
class to compare u1 and u2?
bool Compare(Object obj1,Object obj2,String Operator)
{

.....?
}

Nov 17 '05 #1
6 15612
Locia <ro********@yahoo.it> wrote:
How can I understand if two object obj1 and obj2 have same type?

Can I do: if (obj1.GetType()==obj2.GetType()) return true;?
Absolutely.
Moto u1;
Moto u2;

I call Compare(u1,u2,">")

class Moto have override operator >.

How can I use reflection in Compare fuction to call operator > of Moto
class to compare u1 and u2?


MethodInfo method = typeof(Moto).GetMethod("op_GreaterThan",
BindingFlags.Public |
BindingFlags.Static);

That seems to work, although I don't know to what extent it's
guaranteed...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Yes you can do:

if (obj1.GetType()==obj2.GetType())

And For comparing check out the IComparable interface and the Compare
pattern in .Net:

http://msdn.microsoft.com/library/de...classtopic.asp

HTH

Ollie Riches

"Locia" <ro********@yahoo.it> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
How can I understand if two object obj1 and obj2 have same type?

Can I do: if (obj1.GetType()==obj2.GetType()) return true;?

Moto u1;
Moto u2;

I call Compare(u1,u2,">")

class Moto have override operator >.

How can I use reflection in Compare fuction to call operator > of Moto
class to compare u1 and u2?
bool Compare(Object obj1,Object obj2,String Operator)
{

....?
}

Nov 17 '05 #3
I can't use "typeof(Moto)" in Compare function because u1 and u2 can be
two instance of another class.

Nov 17 '05 #4
I can't use "typeof(Moto)" in Compare function because u1 and u2 can be
two instance of another class.
Another way?

Nov 17 '05 #5
I can't use "typeof(Moto)" in Compare function because u1 and u2 can be
instance of other class.
u1 and u2 are generic object.
Another way?

Nov 17 '05 #6
Locia <ro********@yahoo.it> wrote:
I can't use "typeof(Moto)" in Compare function because u1 and u2 can be
instance of other class.
u1 and u2 are generic object.
Another way?


Call GetType() on u1 then.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

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

Similar topics

8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
17
by: bengamin | last post by:
Hi, I have a C# class and two instance of the class; the class have some property. I want to compare the property value of the two instance How should i do? override == ? use delegate ?
4
by: Gaby | last post by:
Hi all, What is the best way to compare 2 (large) ArrayLists filled with an object. Can you please help me? Gaby
11
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
2
by: Locia | last post by:
How can I compare "if argument"? example: if (leftExpression==RightExpression) After parsing I know the type of RightExpression. I suppone that if RightExpression is wrap into " " is a...
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
1
by: Lambda | last post by:
I defined a class: class inverted_index { private: std::map<std::string, std::vector<size_t index; public: std::vector<size_tintersect(const std::vector<std::string>&); };
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.