Dev
Dev

Reputation: 1826

How to toggle bool property

What's the cleanest way of toggling 4 bools. So if I set Bool2 to true all the others are set to false. Similar to a radio button but with these four properties. Could this be done using Ternary Operator/ logical operators?

XAML:

<TextBlock Text=" {Binding Bool1}"></TextBlock>
<TextBlock Text=" {Binding Bool2}"></TextBlock>
<TextBlock Text=" {Binding Bool3}"></TextBlock>
<TextBlock Text=" {Binding Bool4}"></TextBlock>

C#:

bool b1;
public bool Bool1
{
    get { return b1; }
    set
    {
        b1 = value;
        OnPropertyChanged(nameof(Bool1));
    }
}

bool b2;
public bool Bool2
{
    get { return b2; }
    set
    {
        b2 = value;
        OnPropertyChanged(nameof(Bool2));
    }
}

bool b3;
public bool Bool3
{
    get { return b3; }
    set
    {
        b3 = value;
        OnPropertyChanged(nameof(Bool3));
    }
}

bool b4;
public bool Bool4
{
    get { return b4; }
    set
    {
        b4 = value;
        OnPropertyChanged(nameof(Bool4));
    }
}

void Toggle(object ThePropertySetToTrueHere)
{

}

Upvotes: 0

Views: 145

Answers (1)

pm100
pm100

Reputation: 50110

My suggestion - an enum

 public class WhatEver
 { 
      public enum WhatsTrue{
             Thing1,
             Thing2,
             Thing3,
             Thing4
      };
      WhatsTrue Truth;
      void SetTruth(WhatsTrue truth){
        Truth = truth;
      }
  }

Instad of - somewhere in the code going

if(Bool1).....

or

if(!Bool3) ....

do

 if(Truth == WhatsTrue.Thing1) ..

or

 if(Truth != WhatsTrue.Thing3)

Upvotes: 1

Related Questions