-1

I try to get the value from an list by their key. I send the list from one form to another. This is windows form application See my code:

    /* First form! */     
    var list1 = new List<KeyValuePair<string, int>>();
    list1.Add(new KeyValuePair<string, int>("Cat", 1));
    form2 f3 = new form2(connection , list1);


    /* Form2 */
    private IList<KeyValuePair<string, int>> _theList;

    public editAccount(string connection, List<KeyValuePair<string, int>> arr)
    {
        InitializeComponent();
        _theList = arr;

        label1.Text = _theList["Cat"];

    }

I think it is obviously what I try to do, so any help is great!

SOLVED : Thanks tons to stackoverflow user Sriram Sakthivel! Solution:

    /* First form*/
    Dictionary<string, string> openWith = new Dictionary<string, string>(); 
    openWith.Add("cat", "test");
    editAccount f3 = new editAccount(connection, openWith);

    /* Form 2 */
    private Dictionary<string, string> _theList;

    public editAccount(string connection, Dictionary<string, string> arr)
    {
        InitializeComponent();
        _theList = arr;


        label1.Text = _theList["cat"];

    }
TorrBallen
  • 89
  • 1
  • 1
  • 10

4 Answers4

5

If you need to access values via key then you should use keyed collection such as Dictionary. List can be accessed only via index, it doesn't define a indexer which takes string as parameter.

Apart from that, keys are case sensitive by default. If you need a insensitive key you can use this.

Community
  • 1
  • 1
Sriram Sakthivel
  • 67,773
  • 7
  • 96
  • 172
  • Dictionary is something different to List then? So I should replace em? – TorrBallen Aug 09 '14 at 18:29
  • @TorrBallen Dictionary is based on [HashTable](http://en.wikipedia.org/wiki/Hash_table) which provides efficient access to elements via key. If you need to access via key then `Dictionary` is the way to go. – Sriram Sakthivel Aug 09 '14 at 18:34
  • Thanks again, `Dictionary` worked just fine. Updated main post with final code. – TorrBallen Aug 09 '14 at 18:43
1

If you do not care about case you may use String.ToLower or String.ToUpper methods while setting or using your keys.

Also you should use Dictionary<String, Int32> if you want to access your values by string index:

var list1 = new Dictionary<string, int>();
list1.Add(new KeyValuePair<string, int>("Cat".ToUpper(), 1));
form2 f3 = new form2(connection , list1);


/* Form2 */
private IDictionary<string, Int32> _theList;

public editAccount(string connection,IDictionary<string, Int32> arr)
{
    InitializeComponent();
    _theList = arr;

    label1.Text = _theList["cat".ToUpper()].ToString();
}
Eugene Podskal
  • 9,882
  • 5
  • 29
  • 51
  • 1
    `IList` doesn't have indexer with string as parameter. – Sriram Sakthivel Aug 09 '14 at 18:23
  • Actually, Microsoft recommends you use ToUpper(), not ToLower(), when working with case-insensitive things like keys or comparison. http://msdn.microsoft.com/en-us/library/dd465121%28v=vs.110%29.aspx – RenniePet Aug 09 '14 at 18:26
0

If you want to look things up by key you don't want a List<KeyValuePair<string, int>> you want a Dictionary<string, int>. So replace all occurrances of List<KeyValuePair<string, int>> and, as already stated, your key is case sensitive.

for Lists the [n] indexer wants an int and is used to get the n-th item in the List

tolanj
  • 3,581
  • 13
  • 28
0

You should use a dictionary for this, it is optimised for this by hashing the keys for much quicker retrieval.

That said, if you want a list, one option is to perform a linq query on it.

_list.Where(p => p.Key == "cat");
Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
kidshaw
  • 3,189
  • 2
  • 13
  • 25