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:
and the code for the button, which is where the error is at...
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.
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();
}
}
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);
}
}
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.
Comment