473,499 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how compare two generic object

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 String.
else I check if RightExpression is a int.
else In final case, I check that RightExpression is a name of object
defined in my environment.

Environment keep association between name and object instance and is a
dictionary.
From environment and leftExpression string I get leftExpression Object.


How can I compare leftExpression and RightExpression object?

operator can be == != < >
I don't like how write this code.
Exist a way more generic to do it?
I would like a way to avoid thousand case.

bool Compare(Object leftExpression,Object RightExpression,String op)
{
if (RightExpression is int)
{
if (LeftExpression is int)
{
int RightExpression=(int) RightExpression;
int LeftExpression=(int) LeftExpression;
if op.Equals("==")
{
return (RightExpression.Equals(LeftExpression));
}

if op.Equals("<") returnRightExpression<LeftExpression)
...
...

}
else return false;
else if (RightExpression is String)
{
if (LeftExpression is String)
{
String RightExpression=(String) RightExpression;
String LeftExpression=(String) LeftExpression;
.....
}

else // RightExpression and LeftExpression is generic type how
compare?
{
}
}

Nov 17 '05 #1
2 2472
this is a design question, you need to ask yourself what makes your generic
objects the same. If it is the name then just compare names. if it is object
reference or some other quality, then simply test for that. It may help to
overload the equality operator so the check is handled automatically

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------

"Locia" <ro********@yahoo.it> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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 String.
else I check if RightExpression is a int.
else In final case, I check that RightExpression is a name of object
defined in my environment.

Environment keep association between name and object instance and is a
dictionary.
From environment and leftExpression string I get leftExpression Object.


How can I compare leftExpression and RightExpression object?

operator can be == != < >
I don't like how write this code.
Exist a way more generic to do it?
I would like a way to avoid thousand case.

bool Compare(Object leftExpression,Object RightExpression,String op)
{
if (RightExpression is int)
{
if (LeftExpression is int)
{
int RightExpression=(int) RightExpression;
int LeftExpression=(int) LeftExpression;
if op.Equals("==")
{
return (RightExpression.Equals(LeftExpression));
}

if op.Equals("<") returnRightExpression<LeftExpression)
...
...

}
else return false;
else if (RightExpression is String)
{
if (LeftExpression is String)
{
String RightExpression=(String) RightExpression;
String LeftExpression=(String) LeftExpression;
.....
}

else // RightExpression and LeftExpression is generic type how
compare?
{
}
}

Nov 17 '05 #2
I suppose that two generic objects are equal if have same instance.
class Triangol
{
float x;
float y;
float z;
Triangol(float a, float b, float c)
{
x=a;
y=b;
z=c;
}
}

Triangol tr1=new Triangol(1,2,3);
Triangol tr2=new Triangol(1,2,3);

Now the objects tr1 and tr2 have a identical instance.
When I call if(tr1==tr2) I would like get true.

I call the function Compare(Object obj1,Object obj2,String op)
int this case Compare(tr1,tr2,"==");

How can I implement the Compare function?

Nov 17 '05 #3

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

Similar topics

6
1434
by: Steven Bethard | last post by:
So I thought I'd try to summarize a few things here and maybe we can move toward filing a PEP. I'm not really sure I'm the right person to champion it because, as I've mentioned, I usually...
3
3561
by: Lieven | last post by:
I want to make a quicksort algorithm that can take both a vector and a list. This is the code I've got. But I also want to give an argument that will be used for comparing the elements. I am used...
6
15614
by: Locia | last post by:
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...
15
5298
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
13
3790
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
6
1556
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. Now If I want to implement functionallity so I can compare animal with each other or...
26
3587
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
3
10539
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
6
4330
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
7134
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
7180
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,...
0
7229
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...
1
6905
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
5485
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,...
0
3108
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...
0
1429
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
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
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.