Properties validation error

knappster

Active member
Joined
Nov 13, 2006
Messages
42
Programming Experience
Beginner
Hi,

I have a user control which exposes three properties that the user can change - ValuesStart, ValuesEnd, ValuesIncrement. The default values of these are 0, 100, 10 respectively. These are conditional in that ValueStart needs to be less than ValueEnd, ValueEnd less than ValueStart and ValueIncrement greater than 0. I have put this logic into the property setter and have been testing the functionality of this through the designer and in code but when I do the following I get an error saying that ValuesStart has to be less than ValuesEnd:

VB.NET:
control.ValuesStart = 130
control.ValuesEnd = 160

Obviously it will be ok once the ValuesEnd part has been assigned, is there anyway to tell the code to wait until all values are set before validating?

Cheers.
 
I'm sure this is a typo:
These are conditional in that ValueStart needs to be less than ValueEnd, ValueEnd less than ValueStart and ValueIncrement greater than 0.

Double check the logic, I would think that if they have default values it would not have a problem 'til you entered an inappropriate value for a field. Can you show us your code?
 
yep, typo should be greater than...

my code in user control is as follows:

VB.NET:
Private _valuesStart As Integer
<Browsable(True), DefaultValue(0)> _
Public Property ValuesStart() As Integer
     Get
        Return _valuesStart
     End Get
     Set(ByVal value As Integer)
        If (value < _valuesEnd) Then
          _valuesStart = value
        Else
          Throw New ArgumentException("ValuesStart must be less than ValuesEnd.")
        End If
     End Set
End Property

Private _valuesEnd As Integer = 100
<Browsable(True), DefaultValue(100)> _
Public Property ValuesEnd() As Integer
     Get
        Return _valuesEnd
     End Get
     Set(ByVal value As Integer) 
        If (value > _valuesStart) Then
          _valuesEnd = value
        Else
          Throw New ArgumentException("ValuesEnd must be greater than ValuesStart.")
        End If
     End Set
End Property

then when a user sets the values by:

VB.NET:
control.ValuesStart = 130
control.ValuesEnd = 160

it errors as at the point of setting ValuesStart it is greater than ValuesEnd...
 
The problem is that since you have the default as 100, it must use this number before it sets the _valuesEnd. So for a brief period it is larger. When you set the valueStart = 130 it looks at _valuesEnd which is 100 still, the next line of code would fix this, but it can't because we now have an exception.
 
Try the NumericUpDown control or the TrackBar control, they both have Minimum and Maximum properties, how do they solve such a problem?
 
They probably adjust the max when the minimum is set above the max's original value. So in the Set portion of the ValuesStart() property check for the _valuesEnd value.
 
Back
Top