Hi I am getting error Error 1 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?).Error is at the line string def = hash[elem.Word];.Can anyone help me with this .

Thanks

public Hashtable AllWords()
        {
            
            Hashtable hash = new Hashtable();
            SimpleFrenchEnglishDictionary arr = GetList();
            for (int i = 0; i < arr.Count; i++)
            {
                
                SimpleFrenchEnglishEntry elem =(SimpleFrenchEnglishEntry) arr[i];
                if (hash.Contains(elem.Word) == true)
                {
                    string def = hash[elem.Word];
                    def += "/r/n" + elem.Definition;

                }
                else

                hash.Add(elem.Word, elem.Definition);
            }
                return hash;
           
        }

Hashtable is from the pre-generic days and stores keys and values as objects rather than their native types. As a result, you need to unbox the objects when you wish to use them in the manner you are.

string def = (string)hash[elem.Word]; // should do the trick

To avoid that whole boxing/unboxing thing, consider switching to the generic Dictionary<TKey, TValue> class, where you can specify the types of the key and value in the dictionary.

Dictionary<int, string> example1;
Dictionary<string, double> example2; 
Dictionary<Foo, Bar> example3;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.