Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi there

I wanted to:

fill a list with objects

if a objects.name occurs more than once

then

compare the structure of the two

if the both structures are the same

show messagebox (or just something)

else

do nothing


Whats wrong with simply comparison like:

C#
if (oObjList.Contains(oObj))




i thank already in advance
Posted
Updated 18-Oct-11 4:00am
v2
Comments
BobJanova 18-Oct-11 10:08am    
That's two of us now who don't get it. You need to explain more what you mean by 'structures are the same', and what these objects are.

Now here's my solution (keyword LINQ):

C#
public void AddType(PvssType dpt)
{
    if (this.Any(tp => tp.n.Name == dpt.n.Name))return;

    this.Add(dpt);
}
 
Share this answer
 
I am assuming that we are using List <T>[^] to implement your list of objects.

If you read the documentation of the List<T>.Contains Method[^] and IEquatable<T>.Equals Method[^] you will see that your statement will work if all the objects stored in the list implement the IEquatable interface.

Define the Equals function in the objects like:
C#
public override bool Equals(Object obj)

with this implementation you can return false when the objects are of different types, see the sample in the interface documentation.
 
Share this answer
 
v2
Comments
[no name] 18-Oct-11 10:54am    
+5Links and easy explanation.
André Kraak 18-Oct-11 10:57am    
Thank you.
ZeroDigitality 18-Oct-11 10:58am    
me too +5
BobJanova 18-Oct-11 11:20am    
Note that you still actually need to -write- Equals. And if you override Equals, you must also override GetHashCode, and you should (imo at least) override == and != (otherwise 'a == b' and 'a.Equals(b)' give different answers and that's confusing) - see my answer.
Do you control the class of which the objects are instances? If so, you can override Equals and == so that a == b returns what you want. E.g, a trivial example
class Test {
 int a; double b;

 public static operator bool == (Test a, Test b) { return (object)a == null ? (object)b == null : a.Equals(b); }
 public static operator bool != (Test a, Test b) { return !(a == null); }

 public override bool Equals(object o){
  if(o is Test){
   Test test = (Test)o;
   return a == test.a && b == test.b;
  } else return base.Equals(o);
 }

 public override int GetHashCode(){ return a.GetHashCode() ^ b.GetHashCode(); }
}


If not, and you don't know in advance what type the objects will be, you'll need to use reflection, make a sorted list of their fields/properties/etc (whatever you count as 'structure') and compare those.
 
Share this answer
 
Comments
ZeroDigitality 18-Oct-11 9:58am    
Now the solution is definitely the most confusing I've ever read

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900