Can I use StringBuilder elements in foreach?

Since I am using .NET 1.1, I cannot use a generic string list as generics were not part of the language yet. So I am trying to use StringBuilder, but I get this error message:

"foreach statement cannot work with variables of type" System.Text.StringBuilder "because" System.Text.StringBuilder "does not contain a definition for" GetEnumerator "or is not available"

with this code:

public StringBuilder linesToSend;
. . .

foreach (string _line in this.linesToSend)
{
    serialPort.Write(_line);
}

      

Is there something wrong with my code, or is StringBuilder really forbidden from foreach loops? If the latter is String [], my best regression?

+3


source to share


6 answers


A StringBuilder

does not preserve the lines you add. It is simply used to build the final string. Assuming you've already added everything to StringBuilder

, you can do:



// Write the complete string to the serialPort
serialPort.Write(linesToSend.ToString());

      

+2


source


Old question, I know, but something potentially useful:

If each of your lines was built with .AppendLine, or you inserted a new line, you can do



string[] delim = { Environment.NewLine, "\n" }; // "\n" added in case you manually appended a newline
string[] lines = StringBuilder.ToString().Split(delim, StringSplitOptions.None);
foreach(string line in lines){
    // Do something
}

      

+3


source


A StringBuilder

only builds one line, so how could you foreach

get a whole sequence of lines?

If you need to write line by line, perhaps use ArrayList

, add each line of line to this, and foreach

with string

as the type of the variable foreach

( Object

will be wrapped in string

). Or better yet, use StringCollection

(thanks to Anthony Pegma's comment, to the original question, I forgot this class).

But why not upgrade to a newer version of .NET?

+2


source


It is right. A is StringBuilder

meant to help you build one final line of output as others have stated.

If you have a variable number of lines you need to work on, you can use ArrayList

and iterate over it.

ArrayList strings = new ArrayList();
// populate the list
foreach (string str in strings) {
  // do what you need to.
}

      

If you are afraid that the array list might contain other objects (since it is not strongly typed), you can do it safely:

foreach (object obj in strings) {
  string str = obj as string;
  // If null strings aren't allowed, you can use the following
  // to skip to the next element.
  if (str == null) {
    continue;
  }
}

      

+2


source


The loop foreach

works by calling GetEnumerator

from the interface IEnumerable

, which will return an enumerator that it foreach

uses to get the next element of the object.

StringBuilder

does not implement IEnumerable

or IEnumerable<T>

, which allows it to work foreach

. In this case, you're better off using string[]

or StringCollection

, and when you're done, you can concatenate the collection with StringBuilder

.

Example:

StringBuilder stringBuilder = new StringBuilder();
foreach(string line in array)
{
    serialPort.Write(line);
    stringBuilder.Append(line);
}

      

+2


source


What you are looking for is an array of strings if you know the number of elements or a dictionary.

0


source







All Articles