I am kind of C# novice and I am trying to concatenate a string in C# to display the checked result in a textbox followed by a button click. I was able to get the desired output but the code seems it has not followed the DRY principle in SE.
private void button1_Click(object sender, EventArgs e)
{
String result1, result2="";
if (radioButton1.Checked )
{
result1 = radioButton1.Text;
}
else if (radioButton2.Checked)
{
result1 = radioButton1.Text;
}
else if (radioButton3.Checked)
{
result1 = radioButton3.Text;
}
if (checkBox1.Checked)
{
result2 = checkBox1.Text;
}
if (checkBox2.Checked)
{
if (result2 != "")
{
result2 = result2 + "," + checkBox2.Text;
}
else
result2 = checkBox2.Text;
}
if (checkBox3.Checked)
{
if (result2 != "")
{
result2 = result2 + "," + checkBox3.Text;
}
else
result2 = checkBox3.Text;
}
textBox1.Text="You like to shop from "+ result1
+"for clothing styles like "+result2;
}
I am sure there should be a lot clever way of doing this and it would be highly appreciated if someone could provide me a better solution.