Question TextBox "exception" decision

Karlovsky120

New member
Joined
Jan 3, 2016
Messages
3
Programming Experience
1-3
I have this class:

C#:
public class TextBoxInt : TextBox
    {
        public int min;
        public int max;

        public Value<int> value;

        public virtual void update(object sender, EventArgs e)
        {
            int newValue;

            if (int.TryParse(Text, out newValue))
            {
                value.set(newValue);
                Text = value.get().ToString();
            }

            else
            {
                Text = value.get().ToString();
                Focus();
            }
        }

        public TextBoxInt(Value<int> value, int min, int max)
        {
            this.value = value;
            this.min = min;
            this.max = max;

            LostFocus += new EventHandler(update);
        }
    }

This accepts users input and tries to parse it as an int. If it fails, it reverts to the last input.
The class is tied to a Value class which is just a wrapper around int to turn it into a reference type.

So, this class works nicely for 99 out of 100 cases.

In that one case, I have to display durability of an item to a user and let him change it. The problem is that durability isn't stored as uses left, which you'd expect, but as times used, which is probably more confusing for the user. So instead of displaying the real value to the user, I display [max - value.get()] so it displays the durability in uses left form. This leaves me the issue that my class can no longer deal with this. That leaves me two options:

1. To extend TextBoxInt and have it override update() method to one which deals specifically with the inverted case.
2. To add a boolean flag that will be passed into a constructor that will tell it if the value is inverted or not.

I went with the first method since it seemed more clean. The other one forces extra checks for all 100 cases, while actually doing something in only one. But it still seems kind of messy.

My question is is there a third method?
Is there another way I can go about this that has more of an universal feel?
 
Back
Top Bottom