1

Here is my code

   private static ArrayList GetFirstObjectFromDictionary(Dictionary<string, string> dictionary)
    {
        foreach (ArrayList arr in dictionary.Values) //error 
        {
            return arr;
        }
        return null;
    }

It causes error "Cannot convert type 'string' to 'System.Collections.ArrayList'".

2
  • 1
    Your dictionary.Values s type is Dictionary<string,string>.ValueCollection. So you can't cast it directly ArrayList and this code is not look meaningful! Can you explain what you want to do? Commented May 25, 2013 at 18:37
  • @Furkan Ekinci -Actually i want to take every item in dictionary to ArrayList in type of object,Before implementing Dictionary i have used hashtable for the same piece of code,due to performance reason i have changed data structure to Dictionary. Commented May 27, 2013 at 3:40

1 Answer 1

1

You can use KeyValuePair for reach dictionary's items.

private static ArrayList GetFirstObjectFromHashTable(Dictionary<string, string> dictionary)
{
    ArrayList aLst = new ArrayList();

    foreach (KeyValuePair<string, string> pair in dictionary)
    {
        aLst.Add(pair.Value);
    }

    return aLst;
}

This page might help you to understand foreach using with dictionaries.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.