1

I am developing using an electric calculation engine where all the objects are of the type WDataObject.

I have a class to create graphs that works fine with string values, but now I need to create a graph of these WDataObjects.

to see if a WDataObject is equal to other WDataObject I have to use the own method: .isEqual(WDataObject obj)

therefore, if I create a dictionary like

Dictionary<WDataObject , LinkedList<WDataObject>> map = new Dictionary<WDataObject , LinkedList<WDataObject>>( );

I cannot obtain the Values from the keys normally, instead I have to create iterators and manually check the equivalency.

I have this function that works fine:

public void addOneWayRelation ( string node1 , string node2 ) {
            LinkedList<string> adjacent = new LinkedList<string>( );
            try {
                adjacent = map[node1];
            } catch ( System.Collections.Generic.KeyNotFoundException ) {
                map[node1] = adjacent;
            }
            adjacent.AddLast( node2 );
        }

And I need to change the types from string to WDataObject.

I already have the key search

private LinkedList<WDataObject> searchKey ( WDataObject k ) {
            List<WDataObject> Keys = new List<WDataObject>( map.Keys );
            List<LinkedList<WDataObject>> Values = new List<LinkedList<WDataObject>>( map.Values );

            for ( int i = 0 ; i < Keys.Count ; i++ ) {
                if ( Keys[i].IsEqual( k ) ) {
                    return Values[i];
                }
            }
            return null;
        }

The problem is

map[node1] = adjacent;

How do i replace a Value from an specific key in this context?

Any help is appreciated.

user721807
  • 85
  • 1
  • 8
  • 2
    Any good reason for not overriding the `Equals` method? – Thorsten Dittmar Sep 20 '13 at 07:14
  • Why not you use Dictionary – Muhammad Umar Sep 20 '13 at 07:18
  • For search key, you should use : `return map.ContainsKey(k) ? map[k] : null;`. And you should create a equality comparer for the dictionary and pass it as parameter to its constructor. – Ahmed KRAIEM Sep 20 '13 at 07:18
  • Yes the question is similar, I'll try the public class FooEqualityComparer : IEqualityComparer { public int GetHashCode(Foo foo) { return foo.FooID.GetHashCode(); } public bool Equals(Foo foo1, Foo foo2) { return foo1.FooID == foo2.FooID; } } – user721807 Sep 20 '13 at 08:09
  • You don't need equality comparer, just override the methods GetHashCode and Equal in WDataObject. That will do the trick ! – Eramir Sep 20 '13 at 12:29

1 Answers1

0

Thanks to your indications, this code solved the problem:

Dictionary<WDataObject , LinkedList<WDataObject>> map = new Dictionary<WDataObject , LinkedList<WDataObject>>( new EqualityComparer( ) );

Any Code using the dictionary

public class EqualityComparer : IEqualityComparer<WDataObject> {
        public int GetHashCode ( WDataObject obj ) { return obj.GetHashCode( ); }
        public bool Equals ( WDataObject obj1 , WDataObject obj2 ) { return obj1.IsEqual( obj2 ); }
    }
user721807
  • 85
  • 1
  • 8