Enumerating a StringBuilder object

While trying to list all the string values within a stringbuilder object, I got an exception saying -

foreach statement cannot operate on variables of type ‘System.Text.StringBuilder’ because ‘System.Text.StringBuilder’ does not contain a public definition for ‘GetEnumerator’

But finally thought of a workaround. Using an array of string object. For example,


StringBuilder sColumns = new StringBuilder();
foreach (DataColumn col in dt.Columns)
{
sColumns.Append(col.ColumnName);
sColumns.Append(",");
}


string[] nameIDs = sColumns.ToString().Split(',');
foreach (string nameID in nameIDs)
{
Debug.WriteLine(nameID);
}

Advertisement

About irtazaali
Project Manager, Application Developer, and Technical Consultant in a wide variety of business applications. Particularly interested in project and product development using C#.NET, ASP.NET, XAML, Visual Basic with datastores like XML, SQL Server, Oracle, and MySQL. Always interested in migration projects and ventures in product development. Have 12 years experience in commercial software product development. Quite knowledgeable in ROI and infrastructure development operations. Have previously worked as project manager-software products at US and UK based companies providing business intelligence software solutions. Hold MCSD.NET, OCP, and OCA certifications.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.