simplify if then statements

CrakED
01-07-2004, 04:49 PM
Hello,
I am interested in a way to simplify these if statements...
I think there is a way to load an array, but I am not sure how. Basically I need to take a variable with a numeric value and reassign the closest value (rounded down) to a number in a list.
Example:
My qualifying numberrs are 500, 520, 540, 560, 580, 600, 620, 640, etc, etc....
if strVar1 = 623 then strVar2 = 620

Here are the if statements which are now doing the job, howver I'd like to streamline the code.

If strScore > "500" And strScore < "510" Then
strScore = "500"
End If

If strScore >= "510" And strScore < "520" Then
strScore = "510"
End If

If strScore >= "520" And strScore < "530" Then
strScore = "520"
End If

If strScore >= "530" And strScore < "540" Then
strScore = "530"
End If

If strScore >= "540" And strScore < "550" Then
strScore = "540"
End If

If strScore >= "550" And strScore < "560" Then
strScore = "550"
End If

If strScore >= "560" And strScore < "580" Then
strScore = "560"
End If

If strScore >= "580" And strScore < "600" Then
strScore = "580"
End If

If strScore >= "600" And strScore < "620" Then
strScore = "600"
End If

Optikal
01-07-2004, 05:15 PM
Store the list in an array in order, then use a binary search. If you search this forum for binary search (or google) you will find tonnes of examples of how to do it.

CrakED
01-07-2004, 05:54 PM
thanks, you may have set me on the right path. I can store the "qualifying" numbers in a CSV, and use the open command to "read" the file, but I do not know how to select the correct value...I guess loop through the file, but still don't know how to do it...sorry for the ignorance.

AFterlife
01-07-2004, 08:31 PM
thanks, you may have set me on the right path. I can store the "qualifying" numbers in a CSV, and use the open command to "read" the file, but I do not know how to select the correct value...I guess loop through the file, but still don't know how to do it...sorry for the ignorance.

After you learn it it wont be ignorance though. :p

Bucky
01-07-2004, 08:44 PM
You can use a little math to solve your problem.


' Convert the score string into a number
Dim intScore As Integer = System.ConvertToInt32(strScore)

' Check the range of intScore here

' Then round down to the nearest 10 and set it back to the string
strScore = (intScore \ 10) * 10


How's that for streamlining? :)

IceBreakerG
01-10-2004, 05:00 PM
You can always use a select case statement, although thats similiar to an if statement, it'd reduce some of the lines of code needed for each check.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum