473,413 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Foreach cannot convert type

1
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.Collections.Generic.KeyValuePair<string, string>'

Here is the code for the Class:
Expand|Select|Wrap|Line Numbers
  1. class getCharacter
  2.     {
  3.         public string getCharNow()
  4.         {
  5.             string fldr = "\\character.txt";
  6.             fldr = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + fldr;
  7.  
  8.             System.IO.StreamReader objReader;
  9.             objReader = new System.IO.StreamReader(fldr);
  10.  
  11.             Dictionary<string, string> charProfile = new Dictionary<string, string>();
  12.  
  13.             do
  14.             {
  15.                 string getLine = objReader.ReadLine();
  16.  
  17.                 int findSplit = getLine.IndexOf(":");
  18.                 string heading = getLine.Substring(0, findSplit);
  19.                 string headingValue = getLine.Substring(findSplit + 1);
  20.  
  21.                 charProfile.Add(heading.ToString(), headingValue.ToString());
  22.  
  23.             } while (objReader.Peek() != -1);
  24.             objReader.Close();
  25.  
  26.             return charProfile.ToString();
  27.  
  28.         }
  29.     }
  30.  
and the code for the button, which is where the error is at...

Expand|Select|Wrap|Line Numbers
  1. private void getChar_Click(object sender, EventArgs e)
  2.         {
  3.             getCharacter getProfile;
  4.             getProfile = new getCharacter();
  5.  
  6.             string profile = getProfile.getCharNow();
  7.  
  8.             foreach (KeyValuePair<string, string> pair in profile)
  9.             {
  10.                 listBox1.Items.Add(pair.Key + " : " + pair.Value);
  11.             }
  12.         }
  13.  
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.
Jun 15 '15 #1
1 6709
The getCharacter.getCharacter function should return a Dictionary<string,string> not a string. Then the code will work.

Expand|Select|Wrap|Line Numbers
  1. class getCharacter
  2.     {
  3.  
  4.          public Dictionary<string, string> getCharNow()
  5.          {
  6.              string fldr = "c:\\temp\\character.txt";
  7.             // fldr = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + fldr;
  8.  
  9.              System.IO.StreamReader objReader;
  10.              objReader = new System.IO.StreamReader(fldr);
  11.  
  12.              Dictionary<string, string> charProfile = new Dictionary<string, string>();
  13.  
  14.              do
  15.              {
  16.                  string getLine = objReader.ReadLine();
  17.  
  18.                  int findSplit = getLine.IndexOf(":");
  19.                  string heading = getLine.Substring(0, findSplit);
  20.                  string headingValue = getLine.Substring(findSplit + 1);
  21.  
  22.                  charProfile.Add(heading.ToString(), headingValue.ToString());
  23.  
  24.              } while (objReader.Peek() != -1);
  25.              objReader.Close();
  26.  
  27.              return charProfile;
  28.  
  29.          }
  30.  
  31.  
  32.  
  33.     }
  34.  
  35.  private void button1_Click(object sender, EventArgs e)
  36.         {
  37.              getCharacter getProfile;
  38.              getProfile = new getCharacter();
  39.  
  40.              Dictionary<string,string> profile = getProfile.getCharNow();
  41.              foreach (KeyValuePair<string,string> pair in profile)
  42.              {
  43.                  listBox1.Items.Add(pair.Key + " : " + pair.Value);
  44.              }
  45.          }
Aug 31 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Assimalyst | last post by:
Hi, I am trying to write some server side validator code in C#. I want to have a piece of code that can be used by all DropDownLists on a webform, giving a false value if the SelectedIndex is 0....
6
by: Tim Cartwright | last post by:
I have a page that has the login control on it, nothing else. This page inherits from a master page, neither page has any code in it. This page works perfectly when running on the WebDev debug web...
2
by: Paul Hemans | last post by:
I am very new at .Net. I have a small project where I need to manipulate the contents of a web page. I have a form with a web browser control (webBrowser1) on it. Within the...
2
by: keithb | last post by:
A web site published from its development environment (VS Web Server) to an IIS 6.0 site displays this message: Compiler Error Message: CS0030: Cannot convert type 'ASP.login_aspx' to...
2
by: Marcelo Muzilli | last post by:
Howdy all, in my program, I have a ushort variable and a string variable and if I try to compile it, I'm receiving this error message: "Cannot convert type 'string' to 'ushort'". How can I...
6
by: John | last post by:
The following code: int test = 1; bool isTrue = (bool)test; results in a compiler error: Cannot convert type 'int' to 'bool' wtf, any ideas on how to work around this?
1
by: niharnayak2003 | last post by:
Hi All, I am facing the problem in Typecast inside the Generic class. I need a function which will take two param 1:-Xpath 2:- XMLDOC and return me what i will need. here is the code for...
2
by: Paulo | last post by:
DataRow dr = (DataRow)ds.Tables.Rows.ItemArray; Error 1 Cannot convert type 'object' to 'System.Data.DataRow' C:\Documents and Settings\Fabio\Meus documentos\Visual Studio...
2
by: nomad | last post by:
Hi, When trying to serialize a class I keep getting the message below. optionalExtensionTypeOptionalExtension is not mandatory so not sure why this is appearing. If anyone has seen this type...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.