Foreach cannot convert type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Varkin
    New Member
    • Jun 2015
    • 1

    #1

    Foreach cannot convert type

    Greetings. I'm new to C#, writing my first program and cannot figure out why I'm getting an error in my foreach loop. I apologize if I'm using the wrong terminology when points things out.

    So I have a button that calls a Class. The Method reads a file, stores the results in a Dictionary (tried Hashtable first and got the same error). I return the Dictionary variable. Then I try to loop through the results so I can save and reuse them later. For testing I am just trying to put the results into a listbox.

    I'm getting the error: CS0030 Cannot convert type 'char' to 'System.Collect ions.Generic.Ke yValuePair<stri ng, string>'

    Here is the code for the Class:
    Code:
    class getCharacter
        {
            public string getCharNow()
            {
                string fldr = "\\character.txt";
                fldr = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + fldr;
    
                System.IO.StreamReader objReader;
                objReader = new System.IO.StreamReader(fldr);
    
                Dictionary<string, string> charProfile = new Dictionary<string, string>();
    
                do
                {
                    string getLine = objReader.ReadLine();
    
                    int findSplit = getLine.IndexOf(":");
                    string heading = getLine.Substring(0, findSplit);
                    string headingValue = getLine.Substring(findSplit + 1);
    
                    charProfile.Add(heading.ToString(), headingValue.ToString());
    
                } while (objReader.Peek() != -1);
                objReader.Close();
    
                return charProfile.ToString();
                
            }
        }
    and the code for the button, which is where the error is at...

    Code:
    private void getChar_Click(object sender, EventArgs e)
            {
                getCharacter getProfile;
                getProfile = new getCharacter();
    
                string profile = getProfile.getCharNow();
    
                foreach (KeyValuePair<string, string> pair in profile)
                {
                    listBox1.Items.Add(pair.Key + " : " + pair.Value);
                }
            }
    The file I'm reading from has a mixture of strings and int values, so I seem to be using ToString a lot to make sure if works with the Dictonary string string requirement.

    Also, I'm not sure if it matters or not, but the Class is not on the Button Form page (that may be normal or not, don't know). I just wanted to be able to call this Class from anywhere.

    Again, I've very new to C# so I might be totally off on how I'm trying to do this. Any insight would be great. Thanks.
  • Kara Hewett
    New Member
    • Apr 2014
    • 27

    #2
    The getCharacter.ge tCharacter function should return a Dictionary<stri ng,string> not a string. Then the code will work.

    Code:
    class getCharacter
        {
    
             public Dictionary<string, string> getCharNow()
             {
                 string fldr = "c:\\temp\\character.txt";
                // fldr = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + fldr;
     
                 System.IO.StreamReader objReader;
                 objReader = new System.IO.StreamReader(fldr);
     
                 Dictionary<string, string> charProfile = new Dictionary<string, string>();
     
                 do
                 {
                     string getLine = objReader.ReadLine();
     
                     int findSplit = getLine.IndexOf(":");
                     string heading = getLine.Substring(0, findSplit);
                     string headingValue = getLine.Substring(findSplit + 1);
     
                     charProfile.Add(heading.ToString(), headingValue.ToString());
     
                 } while (objReader.Peek() != -1);
                 objReader.Close();
     
                 return charProfile;
     
             }
    
    
    
        }
    
     private void button1_Click(object sender, EventArgs e)
            {
                 getCharacter getProfile;
                 getProfile = new getCharacter();
     
                 Dictionary<string,string> profile = getProfile.getCharNow();
                 foreach (KeyValuePair<string,string> pair in profile)
                 {
                     listBox1.Items.Add(pair.Key + " : " + pair.Value);
                 }
             }
    Last edited by Rabbit; Aug 31 '15, 08:11 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

    Comment

    Working...