473,508 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Foreach cannot convert type

1 New Member
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 6722
Kara Hewett
27 New Member
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
13964
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
14537
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
14779
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
4997
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
17093
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
30127
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
8173
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
10239
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
4659
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
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
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
5625
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,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
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...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.