Click here to Skip to main content
15,909,498 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

Here is my code

class Program
{

public static IADXVoice ADXVoice1 = new Envox.ADXVoice.ADXVoice();
static void Main(string[] args)
{
Hashtable objhashmap = new Hashtable();
objhashmap.Add(8533, ADXVoice1);
ICollection keytest = objhashmap.Values;
ICollection keytest1 = objhashmap.Keys;

foreach (int key in objhashmap.Keys)
{
Console.WriteLine("{0}, {1}", key, objhashmap[key]);
//Console.WriteLine("{0}, {1}", key, objhashmap[key]);
ADXVoice obj=objhashmap[key];// Am getting convert type error as Cannot implicitly convert type 'object' to 'Envox.ADXVoice.ADXVoice'. An explicit conversion exists (are you missing a cast?)

Console.ReadKey();
}
}
}

In above code am not able to assign the hashtable value to the new object created for my dll used in code.

My requirement is taking the objects in hashtable and at needed time i want to assign in other places example shown in above code.

Thanks,
S.Chand Basha
Posted
Updated 12-Nov-14 19:12pm
v2

1 solution

This code makes little sense; you add hard-coded immediate constant like 8533, miss the type cast, and so on. You don't understand strong typing. You could type-cast objhashmap[key]:
C#
ADXVoice obj = (ADXVoice)objhashmap[key];

which is possible if runtime type is assignment-compatible with the type to be cast to. In your code sample, you show adding the value of the type ADXVoice, so, for this value, the typecast will be successful. But why doing so?

Don't use those obsolete non-generic (non-specialized) collection types. Use, for example, System.Collections.Generic.Dictionary<int, ADXVoice>:
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^].

You need to learn inheritance, compile-time types vs runtime types and other basic OOP ideas, as well as generics.

—SA
 
Share this answer
 
Comments
chand5055 13-Nov-14 1:44am    
Thanks Sergey Alexandrovich
Sergey Alexandrovich Kryukov 13-Nov-14 2:03am    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900