Only3lue Only3lue - 17 days ago 7
C# Question

Declaring int variable with from...to range

Can I declare an int variable with a from...to range, so that e.g. the int count can only have the value from 0 to 19? And when there should be a other value out of range added that just nothing happens, no exception, no new value or something else just ignore it.
I need this variable so often in one class e.g. when I need a value from a list, for the forward and backward buttons and in some methods. I don't want to make an if-clause or call a method every time.

private void btnZurueckData_Click(object sender, RoutedEventArgs e)
{
dataFromGuiToObject(index);
index--;
if (index > -1)
{
resetGUI();
setDataToGui(zeichnungen, index);
btnVorData.IsEnabled = true;
}
else
{
btnZurueckData.IsEnabled = false;
index++;
}
}

private void btnVorData_Click(object sender, RoutedEventArgs e)
{
dataFromGuiToObject(index);
index++;
if (index < zeichnungen.Count)
{
resetGUI();
setDataToGui(zeichnungen, index);
btnZurueckData.IsEnabled = true;
}
else
{
btnVorData.IsEnabled = false;
btnSpeichern.IsEnabled = true;
index--;
}
}


That's how I actually handle my problem but I don't think that it's a good way to do this.

Answer

As Alex K already suggested you can write a Property and use the setter like this:

private int count;

public int Count
{
    get { return count; }
    set
    {
        count = (value >= 0 && value <= 19) ? value : count;
    }
}

This will check whether the new value of the variable is within the ranges that you set and if not, it will just remain at the old value. The initial value would be 0 as usual for a normal int;

If you are using Visual Studio type: propfull and press TAB twice. It will auto-generate a stereotypical property for you.

EDIT:

To answer the question from you comment. You can also call methods inside the property-setter. You could just make two methods to regulate which buttons have to be switched on or off:

public partial class Form1 : Form
{
    // this is just to make the code work 
    List<int> zeichnungen = Enumerable.Range(0, 19).ToList();

    private int count;

    public int Count
    {
        get { return count; }
        set
        {
            // if you are inside the bounds
            if (value >= 0 && value <= 19)
            {
                count = value;
                // enable the forward and backward button
                switchButtons();
            }
            else
            {   // you are out of bounds, so check at which side
                // and enable accordingly
                manageButtons(value);
            }
        }
    }
    public Form1()
    {
        InitializeComponent();
    }

In the Click events you can just increase or decrease the variable and leave the logic to the setter and your 2 custom methods

    private void btnVorData_Click(object sender, EventArgs e)
    {
        Count++;
    }

    private void btnZurueckData_Click(object sender, EventArgs e)
    {
        Count--;
    }

    // this method evaluates at which side you are
    private void manageButtons(int value)
    {
        if (value > zeichnungen.Count)
        {
            btnVorData.Enabled = false;
            btnSpeichern.Enabled = true;
        }
        else
        {
            btnZurueckData.Enabled = false;
        }
    }

    // this just enables the forward and backward button and disables the save button
    private void switchButtons()
    {
        btnVorData.Enabled = true; 
        btnZurueckData.Enabled = true;
        btnSpeichern.Enabled = false;
    }

}
Comments