YIdirm
YIdirm

Reputation: 21

containsValues checking Dictionary<type, object> c#

with ECPoint class :

public class ECPoint
{
    private BigInteger p, x, y;

    public BigInteger P
    {
        get { return p; }
        set { p = value; }
    }

    public BigInteger X
    {
        get { return x; }
        set { x = value; }
    }

    public BigInteger Y
    {
        get { return y; }
        set { y = value; }
    }

    public ECPoint(BigInteger p, BigInteger x, BigInteger y)
    {
        this.p = p;
        this.x = x;
        this.y = y;
    }

}

I have a dictionary in C#:

Dictionary<BigInteger, ECPoint> hashTable = new Dictionary<BigInteger, ECPoint>();

and an object of ECPoint Class :

ECPoint gamma = new ECPoint(p, Qx, Qy);

p,Qx,Qy are numerical values

And I did this test :

if (hashTable.ContainsValue(gamma))
{

    BigInteger j = -1;
    foreach (KeyValuePair<BigInteger, ECPoint> s in hashTable)
       {
         if (s.Value.X==gamma.X && s.Value.Y==gamma.Y)
            {
               j = s.Key;
               return m*j;

            }

       }
}

The problem is this test has never given a true value, it is always false, so how to check if the dictionary hashTable contain the values of an object?. Someone can help me, thanks in advance, and I apologize for my English.

Upvotes: 0

Views: 171

Answers (2)

Ygalbel
Ygalbel

Reputation: 5519

You can use FirstOrDefault() linq function to check and get value from hashTable so you don't have to iterate with foreach.

Your code will be like this:

 var data = hashTable.FirstOrDefault(a => a.Value.X == gamma.X && a.Value.Y == gamma.Y);

    if(data != null){
       return data.Key * m;
    }

Upvotes: 1

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68667

You could either implement Equal and GetHashCode or turn ECPoint into a structure so the compare no longer uses a reference comparison.

Upvotes: 0

Related Questions