473,626 Members | 3,310 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 ==RightExpressi on)

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 (RightExpressio n is int)
{
if (LeftExpression is int)
{
int RightExpression =(int) RightExpression ;
int LeftExpression= (int) LeftExpression;
if op.Equals("==")
{
return (RightExpressio n.Equals(LeftEx pression));
}

if op.Equals("<") returnRightExpr ession<LeftExpr ession)
...
...

}
else return false;
else if (RightExpressio n 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 2481
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********@yah oo.it> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
How can I compare "if argument"?
example: if (leftExpression ==RightExpressi on)

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 (RightExpressio n is int)
{
if (LeftExpression is int)
{
int RightExpression =(int) RightExpression ;
int LeftExpression= (int) LeftExpression;
if op.Equals("==")
{
return (RightExpressio n.Equals(LeftEx pression));
}

if op.Equals("<") returnRightExpr ession<LeftExpr ession)
...
...

}
else return false;
else if (RightExpressio n 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
1442
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 eventually replace generic objects with concrete classes, but I'm certainly willing to do some of the work writing it up, etc. If you're interested in helping me, let me know (off-list). Some quotes from this thread: Hung Jung Lu wrote:
3
3572
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 to programming in Lisp/Scheme, where I would just pass a lambda. Is a function object something like that in C++? And in that case, how would I pass the function/operator < (lesser then). //quicksort template<typename BidirectionalIterator>...
6
15621
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 override operator >.
15
5329
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 path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
13
3814
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
6
1566
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 with other animal for example compare a Chicken with a Cow in some way or a Cow with another Cow
26
3607
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 delegate type expression in just the right place and a well-named method means we can almost read the code out loud and understand it without even thinking. Note that since 'boxing' and 'unboxing' is involved (I think), you don't get what you...
3
10550
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 format was different. Basically you can sort a class having members LastName (string), FirstName (string) and Age (int) according to whether you want to sort by Last Name, First Name or Age, using the .Sort function
6
4334
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 computation 2) compare the string binary data to get the similarity or dissimilarity result. The problem is, i already done with the image (jpg) conversion to binary and also try the algorithm structure in C# language, but i having a problem to...
0
8272
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8644
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8514
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7206
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5579
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.