Re: foreach with a string and Hashtable

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Chris Taylor (chris_taylor_za_at_hotmail.com)
Date: 03/01/04


Date: Mon, 1 Mar 2004 22:12:15 +0200

Hi,

The foreach uses the IEnumerable interface to enumerate through the
collection, in the case of a string the IEnumrable interface implementation
enumerates the characters (char) in the string. Your foreach declaration is
attempting to read each character from linetext into the string s, hence
the error "cannot convert 'char' to 'string'".

If I understand what you are trying to achieve here is to extract each word
from the linetext variable rather than each character. In this case you have
basically three options that I can think of off the top of my head. One is
to use the Split() function to split the string on spaces and punctuation
etc, the second would be to use a regular expression (see Regex class) to
split the string into words and finally you could build a parser to parse
the string and split it according to rules that you define.

I would probably consider the Regex route.

Hope this helps

-- 
Chris Taylor
http://dotnetjunkies.com/WebLog/chris.taylor/
"Dayton2" <mbasil@monsanto.com> wrote in message
news:982DD8DF-CF31-418E-A396-C3F9601E1E38@microsoft.com...
> Is it possible to search through a Hashtable by a string using foreach?  I
am new to C# and seem to be struggling with correctly implementing the
foreach statement.  I am attempting to grab each word from a readline and
put them individually in a hashtable.  This is code that I am working
with...
>
> <code>
> Hashtable table = new Hashtable();
> StreamReader sr = File.OpenText(FILE_NAME);
> String linetext = sr.ReadLine();
> int i = 0;
>
> foreach (string s in linetext)
> {
>      table.Add(i, s);
>      i++;
> }
> <\code>
> The error that I am getting is "cannot convert 'char' to 'string'", but I
am never declaring a char???
> Any suggestions???
> Thanks!!


Relevant Pages

  • RfD: Escaped Strings S" (v6.3)
    ... Escape character is case sensitive, ... the S" string can only contain printable characters, ... impact of char in the file word sets. ...
    (comp.lang.forth)
  • Permuting beyond one collection
    ... where the columns represent a char[]. ... string number = Console.In.ReadLine; ... foreach ... ArrayListnewOptions = new ...
    (comp.programming)
  • Re: System.AccessViolationException
    ... internal string theAP; ... DataColumn col = new DataColumn; ... private string _pwd; ... foreach ...
    (microsoft.public.dotnet.languages.csharp)
  • CfV: Escaped Strings S"
    ... Escape character is case sensitive, ... the S" string can only contain printable characters, ... in the Forth 200x internationalisation and XCHAR proposals. ... impact of char in the file word sets. ...
    (comp.lang.forth)
  • Re: finding strings in a text file help
    ... digits and reserved words and then prints them out in order ... > it gets the whole string matches it against the reserved words array ... one character of a potential word in your "s" string. ... a char[] array would do. ...
    (comp.lang.java.help)