Hi, I'm having some trouble with trouble with an If statement in my program. It's a series of wind speed brackets to select the category from the Saffir Simpson Scale of hurricane classification. The problem I'm having is that it won't select the correct category based on the wind speed entered, e.g If I type 70 it will select cat 4, not cat 0.

The code for the if statements is:

``````'select correct category based on wind speed and store in array
Dim sCategory As Integer

'no category or category one statement
If txtWindSpeed.Text <= 73 Then
sCategory = 0
ElseIf txtWindSpeed.Text >= 74 Then
sCategory = 1
ElseIf txtWindSpeed.Text <= 95 Then
sCategory = 1
End If

'category two statement
If txtWindSpeed.Text >= 96 Then
sCategory = 2
ElseIf txtWindSpeed.Text <= 110 Then
sCategory = 2
End If

'category three statement
If txtWindSpeed.Text >= 111 Then
sCategory = 3
ElseIf txtWindSpeed.Text <= 130 Then
sCategory = 3
End If

'category four statement
If txtWindSpeed.Text >= 131 Then
sCategory = 4
ElseIf txtWindSpeed.Text <= 155 Then
sCategory = 4
End If

'category five statement
If txtWindSpeed.Text >= 156 Then
sCategory = 5
End If``````

I've tried as one big 'If Else If' statement but that didn't work at all. Really confused, its for a college assignment by the way, I'm a beginner!

Any help would be greatly appreciated! Thanks.

Edit: thinking about it more, would I better off using a select case statement for it?

The reason it returned "4" instead of "0" was because when you entered "70". It was smaller than "73" so sCategory was assigned "0". But it is also smaller than "95" which made sCategory "1". The next if statement made it "2" because it is also smaller than "110" and so on.

Try this:

``````If txtWindSpeed.Text <= 73 Then
sCategory = 0
ElseIf txtWindSpeed.Text >= 74 Then
sCategory = 1
ElseIf txtWindSpeed.Text > 74 AND txtWindSpeed.Text <= 95 Then
sCategory = 1
End If

'category two statement
If txtWindSpeed.Text >= 96 Then
sCategory = 2
ElseIf txtWindSpeed.Text > 96 AND txtWindSpeed.Text <= 110 Then
sCategory = 2
End If

'category three statement
If txtWindSpeed.Text >= 111 Then
sCategory = 3
ElseIf txtWindSpeed.Text > 111 AND txtWindSpeed.Text <= 130 Then
sCategory = 3
End If

'category four statement
If txtWindSpeed.Text >= 131 Then
sCategory = 4
ElseIf txtWindSpeed.Text > 131 AND txtWindSpeed.Text <= 155 Then
sCategory = 4
End If

'category five statement
If txtWindSpeed.Text >= 156 Then
sCategory = 5
End If``````

Ah I understand, thanks for the explanation. I copied the one you gave and it seems to be working fine.

Now onto the next step!

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.