0

here is my code

 public bool DisplayError1
    {
        get { return _displayError1; }
        set
        {
            if (value.Equals(_displayError1)) return;

            _displayError1 = value;
            NotifyPropertyChanged();
        }
    }
    private bool _displayError2 = false;
    public bool DisplayError2
    {
        get { return _displayError2; }
        set
        {
            if (value.Equals(_displayError2)) return;

            _displayError2 = value;
            NotifyPropertyChanged();
        }
    }
    private bool _displayError3 = false;
    public bool DisplayError3
    {
        get { return _displayError3; }
        set
        {
            if (value.Equals(_displayError3)) return;

            _displayError3 = value;
            NotifyPropertyChanged();
        }
    }

    private bool _displayError4 = false;
    public bool DisplayError4
    {
        get { return _displayError4; }
        set
        {
            if (value.Equals(_displayError4)) return;

            _displayError4 = value;
            NotifyPropertyChanged();
        }
    }

these are set of my properties. I need to convert this property into a method in order to get the exact one I need

as an example

   void display(string property){}

then I need to get that exact propety in the main method

display(displayerror)

how to do this

I implement this code

   public bool DisplayRightOne(bool display)
    {

        bool[] array = new bool[] {DisplayError,DisplayError1,DisplayError2,DisplayError3,DisplayError4,DisplayError5,DisplayError6,DisplayError7,DisplayError8 };

        int i;
        for( i=0;i<array.Length; i++)
        {
            if (display == array[i])
            {
                return array[i] = true;
            }
        }

        return true;}

I access this method

     DisplayRightOne(DisplayError);

but did not make DisplayError property true.where is the problem pls give me suggestion

1 Answers1

0

The first change I would make is to cut out repeated code. For example, all you property setters do the same thing, but with different variables:

set
{
    if (value.Equals(<VARIABLE_NAME>)) 
        return;

    <VARIABLE_NAME> = value;
    NotifyPropertyChanged();
}

You can turn this into a method, and replace all your setters with it:

set { SetProperty(ref <VARIABLE_NAME>, value); }

...

void SetProperty<T>(ref T variable, T value) 
{
    if (value.Equals(variable)) 
        return;

    variable = value;
    NotifyPropertyChanged();
}

You could then use this method for updating any property, and it will make your code more concise, more manageable, and more readable.

If you're trying to display the correct message given a certain criteria, I would use enums with appropriate names to determine the message you want to display and a switch/case conditional statement:

enum DisplayMessageEnum
{
    Success,
    NetworkConnectionFail,
    NetworkTimeout,
    InvalidCredentials,
    // Repeat as necessary
}

String _displayMessage = "Success";
public String DisplayMessage
{
    get => _displayMessage;
    get => SetProperty(ref _displayMessage, value);
}

...

void SetDisplayMessage(DisplayMessageEnum displayMessage)
{
    switch (displayMessage)
    {
        case DisplayMessageEnum.Success:
            DisplayMessage = "Success";
            break;

        case DisplayMessageEnum.NetworkConnectionFail:
            DisplayMessage = "Network connection failed.";
            break;

        // Repeat for all enum values.
    }
}
Tom
  • 1,739
  • 15
  • 24
  • thank you, in here you used generic type to clean up the code or it is necessary to create this method –  Feb 20 '19 at 10:50
  • It's not necessary, but it makes it more generic for any type. You could replace it with `bool` for example, but then the method could only be used with properties of type Bool. – Tom Feb 20 '19 at 13:15