Most efficient way to validate multiple textboxes against a tolerance value I have a series of text boxes in a table to gather input as below:



The user will input a target and actual value for each measurement point they require. I would then like to validate the actual values against the target values based upon the tolerance inputted in the tolerance textbox. The tolerance will be the same for all measurement points inputted, however the user will not always input all 10 measurement points.

I have also created a very basic class containing a function that accepts the target, actual and tolerance values then returns a Boolean depending on whether the actual value is within tolerance. I realise I could use this with a ruck load of if statements to check each textbox for input then use the class to perform the validation, however this seems like a lot of code repetition and a bit crude. My question being is there a better way I can perform this validation?

Class content

Public Class TolerenceHelper

Public Function IsInTolerence(ByVal target As Integer, ByVal actual As Integer, ByVal tolerence As Integer) As Boolean

    Dim upper As Integer = target + tolerence
    Dim lower As Integer = target - tolerence

    If actual &lt; lower OrElse actual &gt; upper Then
        Return False
    Else
        Return True
    End If
End Function


Calling the function as below:

Dim m1 As TolerenceHelper
Dim flag As Boolean = True

m1 = New TolerenceHelper

If m1.IsInTolerence(Integer.Parse(txtT1.Text), Integer.Parse(txtA1.Text), Integer.Parse(txtTolerance.Text)) = False Then
    flag = False
End If

If flag = False Then
    lblTest.Text = "Out of tolerance"
Else
    lblTest.Text = "In tolerance"
End If
I would write it like this

Dim m1 As TolerenceHelper
Dim flag As Boolean = True

m1 = New TolerenceHelper

flag = m1.IsInTolerence(Integer.Parse(txtT1.Text), Integer.Parse(txtA1.Text), Integer.Parse(txtTolerance.Text))

If flag = False Then
    lblTest.Text = "Out of tolerance"
Else
    lblTest.Text = "In tolerance"
End If


that function returns a boolean so you can just assign it the flag or you could do it like this and avoid creating the flag boolean as well

Dim m1 As TolerenceHelper
m1 = New TolerenceHelper

If m1.IsInTolerence(Integer.Parse(txtT1.Text), Integer.Parse(txtA1.Text), Integer.Parse(txtTolerance.Text)) Then
    lblTest.Text = "In tolerance"
Else
    lblTest.Text = "Out of tolerance"
End If




In your Function IsInTolerence you could eliminate some variables by writing it like this:  

Public Function IsInTolerence(ByVal target As Integer, ByVal actual As Integer, ByVal tolerence As Integer) As Boolean

    If (target - tolerence &lt;= actual AND actual =&lt; target + tolerence)
        Return True
    Else
        Return False
    End If
End Function


Straight to the point.  

check if it is true first, and then dump out in all other cases, this would cover a null or something else.



Remove Code Duplication with a list of TextBox Objects

I am thinking that you create a list of the text boxes, then you should be able to loop through that list, checking each text box for the following.


if it has a value entered
if the tolerance is met
some kind of highlighting to show that the tolerance was not met.


I am not sure on the syntax, but this would be a good way to do it, because you would only have to type out the logic once and then loop through the list of TextBoxes,  and it would be more maintainable because you could just add TextBoxes any time you like and it shouldn't affect the any of the other code.

dim TextBoxListActual as New List(Of TextBox)
TextBoxListActual.Add(txtT1)
TextBoxListActual.Add(txtT2)
TextBoxListActual.Add(txtT3)

Dim TextBoxListTarget as New List(Of TextBox)
TextBoxListTarget.Add(txtA1)
TextBoxListTarget.Add(txtA2)
TextBoxListTarget.Add(txtA3)

dim m1 As TolerenceHelper
m1 = New TolerenceHelper

For index As Integer = 0 To TextBoxListActual.Count
    If m1.IsInTolerence(Integer.Parse(TextBoxListTarget(index).value), Integer.Parse(TextBoxListActual(index).value), Integer.Parse(txtTolerance.Text))
        lblTest.Text = "In Tolerance"
    Else
        lblTest.Text = "Out of Tolerance"
    End If
Next


little bit of an example.  probably not perfect.