how compare two generic object

  • Thread starter Thread starter Locia
  • Start date Start date
L

Locia

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?
{


}


}
 
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
 
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top