Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 15, 2009 02:17 AM by MetalAsp.Net
Apr 14, 2009 10:58 PM|LINK
I have code to generate a new random number as follows (which is 100% working and generating numbers between 0-9:
Dim randomNumber As Integer
Dim randomNumber1 As New Random
randomNumber = randomNumber1.Next(0, 9)
I then have some if statements as follows HOWEVER, EVEN if the randomNumber is GREATER than 2 it still DISPLAYS in variable1 "CAKE" instead of either PIZZA or PASTA. It is strange as it is definately generating numbers greater than 2.
If (randomNumber = 0) Then
variable1 = "biscuit"
ElseIf (randomNumber = 1 Or 2) Then
variable1 = "cake"
ElseIf (randomNumber = 3 Or 4) Then
variable1 = "pizza"
ElseIf (randomNumber > 4 & randomNumber < 8) Then
variable1 = "pasta"
Apr 15, 2009 01:42 AM|LINK
Not a VB expert, but I think it should be:
ElseIf (randomNumber = 1
Or randomNumber =
Also, I don't know if you can use &, I think you need to use "And" (for the pasta one).
Apr 15, 2009 01:49 AM|LINK
I managed to fix this by removing the ELSE if's and replaced them all with IF's and END IF'S so its 5 different IF statements instead of it iterating through the else's!
Apr 15, 2009 02:17 AM|LINK
You may not care about performance, but if you do, then what you're doing is somewhat detrimental because you force the code to run through ALL the ifs that you have. With the Else If, as soon as one of the if/else if is true, the rest of the else ifs are
skipped, making for better performing code. Not critical [:)] but in case you care...