Reputation: 23
Can i make short that my code IF statement in one single IF Statement?
If randomNumber = strWords2(StrwrVal.Text) Then
Else
If randomNumber = strWords3(StrwrVal.Text) Then
Else
If randomNumber = strWords4(StrwrVal.Text) Then
Else
If randomNumber = strWords5(StrwrVal.Text) Then
Else
TxtRnd1.Text = TxtRnd1.Text & vbNewLine & randomNumber
End if
End if
End if
End if
Upvotes: 0
Views: 337
Reputation: 54417
You should NEVER have an empty If
block. If you don't want to do something if a condition is True
, don't test whether that condition is True
in the first place. Test for the inverse condition. In your case, you should be doing this:
If randomNumber <> strWords2(StrwrVal.Text) AndAlso
randomNumber <> strWords3(StrwrVal.Text) AndAlso
randomNumber <> strWords4(StrwrVal.Text) AndAlso
randomNumber <> strWords5(StrwrVal.Text) Then
TxtRnd1.AppendText(Environment.NewLine & randomNumber)
End if
Upvotes: 3
Reputation: 3207
Instead of a bunch of Else
and If
, or a bunch of ElseIf
, you can use a Select Case
. It's easier to read, among other things.
The Select Case
will evaluate a variable and you can choose the outcome depending on what you find. You can also test for True
if you want to evaluate things more complicated than one variable.
Select Case randomNumber
Case strWords2(StrwrVal.Text)
'some code
Case strWords3(StrwrVal.Text)
'some other code
Case strWords4(StrwrVal.Text)
'you got the idea
Case Else
TxtRnd1.Text = TxtRnd1.Text & vbNewLine & randomNumber
End Select
For what I read this would be the cleanest answer, but it always depends on the algorithm. Have fun!
Upvotes: 1