Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# .NET4
Hi there
I wanted to:
fill a list with objects
if a occurs more than once
compare the structure of the two
if the both structures are the same
show messagebox (or just something)
do nothing

Whats wrong with simply comparison like:
if (oObjList.Contains(oObj))

i thank already in advance
Posted 18-Oct-11 3:52am
Edited 18-Oct-11 5:00am
BobJanova at 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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:
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.
Collin Jasnoch at 18-Oct-11 10:54am
Links and easy explanation.
André Kraak at 18-Oct-11 10:57am
Thank you.
hyperlinx at 18-Oct-11 10:58am
me too +5
BobJanova at 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
hyperlinx at 18-Oct-11 9:58am
Now the solution is definitely the most confusing I've ever read
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Now here's my solution (keyword LINQ):
public void AddType(PvssType dpt)
    if (this.Any(tp => tp.n.Name == dpt.n.Name))return;

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

  Print Answers RSS
0 Zoltán Zörgő 185
1 Sergey Alexandrovich Kryukov 160
2 Peter Leow 100
3 CPallini 70
4 Ramza360 51
0 Zoltán Zörgő 80
1 the other paul 23
2 Shmuel Zang 10
3 Aarti Meswania 5
4 Member 11389991 -2

Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 21 Oct 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100