Solved

Cannot convert from object to string, dictionary c#

Posted on 2010-11-14
2
918 Views
Last Modified: 2012-06-27
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.'


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();

        }
    }

Open in new window

0
Comment
Question by:doramail05
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34134058
Try changing:

    dict.Add(myStringArray[o], myIntArray[o]);

To:

    dict.Add((string)myStringArray[o], (int)myIntArray[o]);
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 34134175
FYI:
To increase the perfermance use List<T> instead of ArrayList
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

634 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question