Solved

# More "classy" questions

Posted on 2005-05-17

My functions and classes are increasing in complexity and I'm not sure how to tackle certain problems.

I have a form with 1 drop down list (called "Group Type") and 4 text boxes where people can enter the number of travellers for each age group.

Now I need to validate these text boxes in certain ways. For example I need to check that if a certain group type is chosen (for example "Couple") then there has be at least 2 adults specified in the text boxes.

I have created a function to validate all this:

Public Shared Function ValidTravellers(ByVal intAdult As Integer, ByVal intDependent As Integer, ByVal strGroupType As String, ByRef messageOut As String) As Boolean

Dim IsValid As Boolean = True

Dim intAll As Integer = (intAdult + intDependent)

If intAll = 0 Then

messageOut = "Please state how many people of each age group are travelling"

IsValid = False

Else

If strGroupType = "1" Then ' Group Type is Individual

If intAll > 1 Then

messageOut = "You have chosen cover for a single person but have requested cover for more than 1 person"

IsValid = False

End If

ElseIf strGroupType = "2" Then ' Group Type is Couple

If intAdult <> 2 Then

messageOut = "You have chosen couple cover but have not requested cover for 2 adults. The definition of couple is ""insured and partner living at the same address""."

IsValid = False

ElseIf intDependent <> 0 Then

messageOut = "You have chosen couple cover but have requested cover for children. If you require cover for children then please select the Family policy."

IsValid = False

End If

ElseIf strGroupType = "3" Then ' Group Type is Family

If intAdult <> 2 Then

messageOut = "You have chosen family cover but have not requested cover for 2 adults. The definition of family is 2 adults and 4 dependant children."

IsValid = False

ElseIf intDependent > 4 Then

messageOut = "You have chosen family cover but have requested cover for more than 4 children. The definition of family is 2 adults and 4 dependant children."

IsValid = False

ElseIf intDependent = 0 Then

messageOut = "You have chosen family cover but have requested cover for 0 children. The definition of family is 2 adults and 4 dependant children."

IsValid = False

End If

ElseIf strGroupType = "4" Then ' Group Type is Single Parent Family

If intAdult <> 1 Then

messageOut = "You have chosen single parent family cover but have not requested cover 1 adult. The definition of single parent family is 1 adult and up to 4 dependant children"

IsValid = False

ElseIf intDependent > 4 Then

messageOut = "You have chosen single parent family cover but have requested cover for more than 4 children. The definition of family is 2 adults and 4 dependant children"

IsValid = False

ElseIf intDependent = 0 Then

messageOut = "You have chosen single parent family cover but have requested cover for 0 children. The definition of family is 2 adults and up to 4 dependant children"

IsValid = False

End If

End If

End If

Return IsValid

End Function

I have to pass to this function the quantities of adults ("intAdult") and dependents ("intDependent").

To get these quantities I have to add together the contents of the textboxes, so I use the following 2 functions to do this:

Public Shared Function CountAdult(ByVal intUnder65 As Integer, ByVal intUnder70 As Integer, ByVal intUnder75 As Integer, ByVal intUnder81 As Integer, ByVal intUnder90 As Integer) As Integer

Return (intUnder65 + intUnder70 + intUnder75 + intUnder81 + intUnder90)

End Function

Public Shared Function CountDependent(ByVal intUnder18 As Integer, ByVal intUnder3 As Integer, ByVal intStudent As Integer) As Integer

Return (intUnder18 + intUnder3 + intStudent)

End Function

These 2 functions just add together the numbers specified in the text boxes.

However, if a text box is left empty then the code falls apart becuase the function needs a number for each variable.

So perhaps I need another function that checks whether the text box has a value and if it doesn't then assign it the quantity of 0. i.e.

If txtUnder65.Text = "" Then intUnder65 = 0

But then I'm kind of piling function on top of function and things are becoming increasing (and perhaps needlessly) complex.

I hope someone can understand this and point me towards the path of simplicity and enlightenment.

Regards

Stephen