XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

List.Contains problem

Last post 4/6/2009 2:51 AM by Sorcerer. 2 replies.
  • 4/6/2009 12:19 AM

    List.Contains problem


    Ok, I have these nodes that I want to put on a list, but there are some repeated nodes and I tried to use the List.Contains, but it is not working because all the repeated nodes are going into the list. And I can not see what I am doing wrong.

    Here is the code and the values of two repeated nodes:


    for (int i = 0; i < collD.CollisionBBoxPathNodes.Count; i++)  
                {  
                    BoundingBox bb = collD.CollisionBBoxPathNodes[i].BBoundingBoxValue;  
                    string xnaNodeName = collD.CollisionBBoxPathNodes[i].XnaNodeNameValue;  
                    string xnaNextNodeName = collD.CollisionBBoxPathNodes[i].XnaNextNodeNameValue;  
                    string xnaPathName = collD.CollisionBBoxPathNodes[i].XnaPathNameValue;  
                    Vector3 xnaNodePosition = collD.CollisionBBoxPathNodes[i].XnaNodePosition;  
                    CollisionBBPathNode node = new CollisionBBPathNode(xnaNodeName, xnaNextNodeName, xnaPathName, xnaNodePosition, bb, CollisionType.CType.BBPathNode);  
     
                    if (!bbScenePathNodes.Contains(node))  
                        bbScenePathNodes.Add(node);  
                }  
     
     
    // And printing the values of the two equal nodes the result will be this:

    {Min:{X:1298,639 Y:353,1761 Z:-24,5325} Max:{X:1347,704 Y:402,2411 Z:24,5325}}  
    BBPathNode  
    {X:1312,769 Y:375,4126 Z:4,554965}  
    Path1  
    Node8  
    Node9  
     
    {Min:{X:1298,639 Y:353,1761 Z:-24,5325} Max:{X:1347,704 Y:402,2411 Z:24,5325}}  
    BBPathNode  
    {X:1312,769 Y:375,4126 Z:4,554965}  
    Path1  
    Node8  
    Node9 

    Any ideas ?

    Thanks.
    http://www.youtube.com/alexmbr
  • 4/6/2009 1:42 AM In reply to

    Re: List.Contains problem

    The contains method used the default Object.Equals() method to compare to objects by reference. This only checks to see if the objects point to the same location in memory, not whether the values the objects hold are the same.

    To compare the fields of the class you need to override the default Equals method.

    public class CollisionBBPathNode(...) 
         public override bool Equals(object o) 
         { 
              CollisionBBPathNode n = o as CollisionBBPathNode; 
              //do some comparisons 
         } 

  • 4/6/2009 2:51 AM In reply to

    Re: List.Contains problem

    I didn't know that.

    Thanks
    http://www.youtube.com/alexmbr
Page 1 of 1 (3 items) Previous Next