-4

I trying to find a proper way to find key in the dictionary with the class attribute.

for example see the code below - the ContainsKey is in erro becuse ther is not implementation for this ContainsKey(string) signature while the key is UserServiceData.

How can I solve this?

class UserServiceData 
{
    string name;
    int someDate;
    int someData1;
}

class B
{
    public static Dictionary<UserServiceData, IClientKdcCallBack> users_list = new Dictionary<UserServiceData, IClientKdcCallBack>;

    void isUserExists(string userName)
    {
        m_users_list.ContainsKey(userName)
    }
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
LIOR
  • 149
  • 1
  • 12

3 Answers3

0

You can also try writing your own equality comparer.

private class UserServiceDataEqualityComparer : IEqualityComparer<UserServiceData>
{
    public bool Equals(UserServiceData x, UserServiceData y)
    {
        return x.name == y.name;
    }

    public int GetHashCode(UserServiceData obj)
    {
        return obj.name.GetHashCode();
    }
}

And then declare your dictionary like this..

var list = new Dictionary<UserServiceData, IClientKdcCallBack>(new UserServiceDataEqualityComparer());

And then you can do this

list.ContainsKey(UserServiceDataObj)
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48
  • i tried this way, but what if the input is not the type of the key but some atterbte the the key contains , for example list.ContainsKey(string) ? – LIOR Jun 23 '18 at 14:08
  • @LIOR see the *Equals* method. You can change it anyway you want – L.B Jun 23 '18 at 18:08
  • @LIOR There is no such overload for Dictionary exists. You either need to write your own extension method that takes string as an argument and write your own custom logic suggested in other answers, or go with my solutions. It is up to you. – Sateesh Pagolu Jun 25 '18 at 02:21
-1

Assuming the "name" property of your "UserServiceData" class will be unique within the Dictionary itself, you can do:

users_list.Any(x => x.Key.name == userName);

If two instances of UserServiceData exist with the same userName, it will still return true, so be careful.

Chad H
  • 584
  • 3
  • 11
-2

Add using

using System.Linq;

then

public void isUserExists(string userName)
{
   bool result = m_users_list.Keys.Any(x => x.name == userName);
}

Note that all fields of UserServiceData is private! To make this work you need make it, at least public:

 class UserServiceData 
 {
    public string name;
    public int someDate;
    public int someData1;
 }

So working code is:

class UserServiceData
{
    public string Name   { get; set; }
    public int SomeDate  { get; set; }
    public int SomeData1 { get; set; }
}

class B
{
    public static IDictionary<UserServiceData, IClientKdcCallBack> users_list = new Dictionary<UserServiceData, IClientKdcCallBack>();

    public bool isUserExists(string userName)
    {
        return users_list.Keys.Any(x => x.Name.Equals(userName));
    }
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • 1
    The purpose of using a dictionary is to access the items faster ( in `O(1)` time). But you iterate on the whole KeyValue pairs with (`m_users_list.Keys.Any(x => x.name == userName);`) (*Of course if an item is found it can return before looping whole dict*). – Eser Jun 23 '18 at 17:59