String building based on user selection
I have a series of checkboxes on a winform. Based on check box selection, application have to build a string开发者_开发问答.
What is best way of coding for problem.
thanks in advance.
-Harsha
Sol 1: Check every check box in individual 'if' statements and then build the string using string builder. But problem is we will have so many if statments.
Create List.
List<CheckBox> list = new List<CheckBox>()
{
checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6
};
var result = (from checkbox in list
select string.Format("{0} is {1}",
checkbox.Name, checkbox.Checked ? "Checked" : "Unchecked")).ToArray();
string str=string.Join(",",result);
OR
foreach(string s in result) {
//
}
Hmm... if there is many strings you can use StringBuilder for better performance rather than simple concatenation.
精彩评论