Forum Home
Press F1
 
Thread ID: 58571 2005-06-04 23:29:00 Visual Basic 6 Dannz (1668) Press F1
Post ID Timestamp Content User
361240 2005-06-04 23:29:00 I have this code and i am wondering if i can condense it with a loop etc




Sub Check
'Checks which of the 3 criteria have met the requirements
intAge = txtAge.Text
intIncome = txtIncome.Text
intExperience = txtExperience.Text

If intAge <25 Then
strAge = "Passed"
Else
strAge = "Failed"
strMessage = strMessage & "You are too old" & vbCrLf
End If

If intIncome >40000 Then
strIncome = "Passed"
Else
strIncome = "Failed"
strMessage = strMessage & "Your income is too low" & vbCrLf
End If

If intExperience >5 Then
strExperience = "Passed"
Else
strExperience = "Failed"
strMessage = strMessage & "You do not have enough experience" & vbCrLf
End If

End Sub
Dannz (1668)
361241 2005-06-04 23:41:00 you could make a function, pass it the value your wanting to check (i.e. for the age it is 25), and pass it the strMessage you want. So your code would look like:
checkCriteria(25,"you are too old",true);
checkCriteria(40000, "your income is too low",false);
checkCriteria(5, "you do not have enough experience",false);

where the true is for use the less than condition, and the false is to use the greater than condition.

In reality its not really saving you any time, just leave it as it is!

- David
DangerousDave (697)
361242 2005-06-05 00:58:00 Thanks, i will just leave it as it is Dannz (1668)
361243 2005-06-05 05:11:00 That's pretty compact code as it is - but building on DangerousDave's idea, you could have one function that checks all values and returns the met criteria.

But that would probably end up in more code.
Growly (6)
361244 2005-06-05 08:07:00 Sub Check
'Checks which of the 3 criteria have met the requirements
intAge = txtAge.Text
intIncome = txtIncome.Text
intExperience = txtExperience.Text

If intAge >24 Then
strAge = "Failed"
strMessage = strMessage & "You are too old" & vbCrLf
End If

If intIncome <40001 Then
strIncome = "Failed"
strMessage = strMessage & "Your income is too low" & vbCrLf
End If

If intExperience <6 Then
strExperience = "Failed"
strMessage = strMessage & "You do not have enough experience" & vbCrLf
End If

End Sub

If you use the 3 variables elsewhere, you could set the default value for the 3 of them to "Passed" before the first if.
14_with_black_bean_sauce (7493)
361245 2005-06-06 04:44:00 There is a "case" command, which is far better than multiple if statements.
Sorry I don't have a copy, but you can find it easy enough
netchicken (4843)
361246 2005-06-06 06:00:00 Case wouldnt work as you are checking the value of 3 different varibles, but if you really want to get technical and you really want to use a loop then Load an array and check the values in each element of the array.

fun, fun, joy, joy

other wise that code snippet is fine as it is.
beama (111)
361247 2005-06-06 20:22:00 You're right beama :)
I didn't look at the example closely enough...

I also think for the minor amount of text to run 3 independant IF's then what is there is good enough.
netchicken (4843)
1