Feature Post

Top

Manual comma separation versus the cool CommaDelimitedStringCollection class!

Some time back while looking around for something else, I was stumbled upon this class, and I simply enjoy using it!

A "wow"-class; I would say. Though, not a lot of documentation is available, but it is worth using this class for those who frequently manage in-memory data with lists/arrays/etc.

A not-so-cool code, somewhere in the asp.net page, goes as follows:
ListBox ctl = ((ListBox)someControlHolder.FindControl("objControl" + i.ToString()));
for (int j = 0; j < ctl.Items.Count; j++)
{
   if (ctl.Items[j].Selected)
   str +=ctl.Items[j].Text + ",";//make it comma separated.
}
str.Remove(str.ToString().Length - 1, 1); //remove the last extra comma.

or And for oCommaDelimitedStringCollection reason, a cool/neat/clean is as follows:
ListBox ctl = ((ListBox)someControlHolder.FindControl("objControl" + i.ToString()));
CommaDelimitedStringCollection strCsv = new CommaDelimitedStringCollection();
for (int j = 0; j < ctl.Items.Count; j++)
{
  if (ctl.Items[j].Selected)
  {
   strCsv.Add(ctl.Items[j].Text);
  }
}

And yes, you are right, doing strCsv.ToString() will write the comma separated string (0:

Enjoy...!