VB.net Si o declaración

I have a textbox that people enter a number or a range, eg 12-15, and a random number is generated. Currently if the second number is less than the first I can get it to work how I want but not if only a single number is listed.

rnum1 should equal rnum2 if there isn't a words(1) or if it's less than words(0). (I do have it done if it's less.)

Dim words As String() = TextBox2.Text.Split("-")
Dim rnum1 As String = words(0)
Dim rnum2 As String = words(1)
Dim RandomClass As New Random()
Dim RandomNumber As Integer

If rnum2 < rnum1 Then
rnum2 = rnum1

End If

RandomNumber = RandomClass.Next(rnum1, rnum2)

preguntado el 23 de septiembre de 13 a las 03:09

1 Respuestas

Cambia esta linea

Dim rnum2 As String = words(1)

a

Dim rnum2 As String = IF(words.Length = 2, words(1), rnum1)

It checks whether there're 2 elements in words array. If it is - it uses second element of array, otherwise it reassigns the first.

Come to think of it, this can be achieved even by this:

Dim rnum2 As String = words(words.Length - 1)

If there're 2 elements in the array - it will assign words(1), otherwise words(0)

Respondido el 23 de Septiembre de 13 a las 03:09

Thanks Yuriy that worked perfectly but it seems my If ... then ... isn't working after all. - gwilson

Try changing type of rnum1 and rnum2 from String to Integer. This way they will be compared as numbers. - Yuri Galanter

That did it! Thanks again. - gwilson

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.