Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET4
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:
 
if (oObjList.Contains(oObj))
 

 
i thank already in advance
Posted 18-Oct-11 3:52am
Edited 18-Oct-11 5:00am
v2
Comments
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
good
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.
  Permalink  
v2
Comments
Collin Jasnoch at 18-Oct-11 10:54am
   
+5
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
good
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.
  Permalink  
Comments
hyperlinx at 18-Oct-11 9:58am
   
Now the solution is definitely the most confusing I've ever read
Rate this: bad
good
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.Add(dpt);
}
 
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 21 Oct 2011
Copyright © CodeProject, 1999-2014
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