0

So in my program, i have three checboxes (A, B and C). and I want to save the content of the checkbox the is checked to a text file. I am doing this using if statements as shown below:

if (a.IsChecked == true)
{
    res = a.Content.ToString() + " is checked"; 
}
else if (b.IsChecked == true)
{
    res = b.Content.ToString() + " is checked";
}
else if (c.IsChecked == true)
{
    res = c.Content.ToString() + " is checked";
}

And here is where i am saving the above values to a string and then later in my code to a text file

string test = res;

Now this is working for me. So i decided to try to check if multiple checkboxes are being checked. So added the below if statements:

else if ((a.IsChecked == true) && (b.IsChecked == true) && (c.IsChecked == true))
{
    res= a.Content.ToString() + " " + b.Content.ToString() + " " + c.Content.ToString()
}

but this isn't working with me because in the end res is printed in the text file as a rather than a b c. Any idea what i am doing wrong?

Also please note that i already initialized res at the top of my code as string:

string res;

I am not getting any error when i run my code so i am not sure where my mistake is. any help with this is much much appreciated.

thanks a lot :)

mm8
  • 163,881
  • 10
  • 57
  • 88
Dr.Bake
  • 141
  • 4
  • 18
  • else if ((a.IsChecked == true) && (b.IsChecked == true) && (c.IsChecked == true)) if any condition fails it 'res' will be null – Ragavan Feb 22 '17 at 12:07

3 Answers3

1

Its a good practice to use a StringBuilder in these cases. On the other hand, if it is ok to have one line for each CheckBox, you can use the following:

 StringBuilder sb = new StringBuilder();
 checkappend(ref sb, a);
 checkappend(ref sb, b);
 checkappend(ref sb, c);
 string res = sb.ToString();

in which

static void checkappend(ref StringBuilder sb, CheckBox ck)
{
    sb.Append(ck.Content.ToString());
    sb.Append(ck.IsChecked == true ?  "is checked." : " is NOT checked.");
    sb.Append(Environment.NewLine);
}

Note that creating a separate class can help you when there are many CheckBoxes in a List. You can simply use

   foreach (var ck in listOfCheckBoxes)
       checkappend(ref ck, c);
rmojab63
  • 3,513
  • 1
  • 15
  • 28
  • Thanks a lot for your answer, but can you explain to me what stringbuilder is? :) – Dr.Bake Mar 05 '17 at 10:31
  • Something similar to ``string``, however, it increases performance. see [this](http://stackoverflow.com/questions/3069416/difference-between-string-and-stringbuilder-in-c-sharp) or [this](http://stackoverflow.com/questions/73883/string-vs-stringbuilder). – rmojab63 Mar 05 '17 at 10:35
0

You can implement it this way:

string res = "";
if (a.IsChecked)
{
    res += a.Content.ToString(); 
}
if (b.IsChecked)
{
    res += b.Content.ToString();
}
if (c.IsChecked)
{
    res += c.Content.ToString();
}

or simple

string res = $"{(a.IsChecked?a.Content+" ":"")}{(b.IsChecked?b.Content+" ":"")}{(c.IsChecked?c.Content:"")}";
RusArt
  • 576
  • 4
  • 12
0

That's multiple combinations to check. Simply remove else from first code snippet to run all checks one after another. You will get only report from last successful check, to have several reports you have to accumulate them somehow (add to a list, combine in multi-line string, etc.).

Here is a simple one-liner (using linq):

var result = string.Join(" and ", new[] { a, b, c }.Where(o => o.IsChecked).Select(o => $"{o.Content} is checked"));
Sinatr
  • 20,892
  • 15
  • 90
  • 319