doramail05
asked on
Cannot convert from object to string, dictionary c#
try to add arraylist from array and add into dictionary (string and integer) and finally sort the value of the dictionary.
while adding to dictionary, getting
'cannot convert from objecct to string.'
while adding to dictionary, getting
'cannot convert from objecct to string.'
static void Main(string[] args)
{
string[] strArray = { "A", "B", "C" };
int[] intArray = { 3, 2, 1 };
ArrayList alstr = new ArrayList();
ArrayList alint = new ArrayList();
for (int m = 0; m < strArray.Length; m++)
{
alstr.Add(strArray[m]);
}
for (int n = 0; n < intArray.Length; n++)
{
alint.Add(intArray[n]);
}
object[] myStringArray = alstr.ToArray();
object[] myIntArray = alint.ToArray();
var dict = new Dictionary<string, int>();
//foreach (string strArray01 in strArray)
//{
// foreach (int intArra01 in intArray)
// {
// dict.Add(strArray01, intArra01);
// break;
// }
//}
for (int o = 0; o < myStringArray.Length; o++)
{
dict.Add(myStringArray[o], myIntArray[o]);
}
//for (int i = 0; i < strArray.Length; i++)
//{
// dict.Add(strArray[i], intArray[i]);
//}
var items = from k in dict.Keys
orderby dict[k] ascending
select k;
foreach (string kv in items)
{
Console.WriteLine("Key : {0}, Value : {1}", kv, dict[kv]);
}
Console.ReadLine();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To increase the perfermance use List<T> instead of ArrayList