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
Solved

Cannot convert from object to string, dictionary c#

Posted on 2010-11-14
2
911 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
2 Comments
 
LVL 85

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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