user3598883
user3598883

Reputation: 47

generate numbers between set values vb.net

I am creating a program to generate numbers between a list of values. Problem I am having is if the first number stats with a smaller value than the second value it will create an error, such as 1000 and 32 will say it doesnt work, or 532 and 64 I do not understand why or how to fix it

Private Sub Generate_Click(sender As System.Object, 
      e As System.EventArgs) Handles Generate.Click

    'Displays to enter correct information if lower is <=
    If Upper.Text <= Lower.Text Or Lower.Text >= Upper.Text Then
        List.Items.Clear()
        List.Items.Add("Please enter correct info Upper # higher value than Lower #")
    Else

        'If Upper range is higher than lower range then display numbers until total value is displayed
        List.Items.Clear()
        Number.Text = ""
        Dim i As Integer = Lower.Text

        Do While i <= Upper.Text 'Loop generates the numbers between values specified
            List.Items.Add(i)
            i += 1
        Loop

        'Select a random value from the list generated
        Dim myRandom As New Random
        Dim b As Integer = List.Items.Count
        Dim chosenItem As System.Object = List.Items.Item(myRandom.Next(b))
        Number.Text = chosenItem.ToString

    End If

End Sub

Upvotes: 1

Views: 782

Answers (2)

Guru Josh
Guru Josh

Reputation: 575

With the If clause, you are doing a string comparison on numerical values. You need to cast them to integer values. And you are doing the same comparison twice which is unnecessary. Also there is a simpler way of generating random numbers between two integers.

I would encourage you to code with the "Option Strict On" declaration at the top of the page. The compiler will alert you when you attempt to make implicit conversions of which there are several in your code.

Dim iMinimum As Integer = Integer.Parse(Lower.Text) 
Dim iMaximum As Integer = Integer.Parse(Upper.Text) 

If iMaximum <= iMinimum Then
    Number.Text = "Please enter correct info Upper # higher value than Lower #"
Else
    Dim randomObject As Random = New Random
    Number.Text = randomObject.Next(iMinimum, iMaximum).ToString
End If

Upvotes: 1

Lalit Shingadiya
Lalit Shingadiya

Reputation: 113

Basically you have to compare numeric value you are comparing string so it is happening.

And 'or' condition is not required in if statement so omit it.

please replace you code as follows.

if System.Convert.ToInt32(Upper.Text) <= System.Convert.ToInt32(Lower.Text) Then
    List.Items.Clear()
    List.Items.Add("Please enter correct info Upper # higher value than Lower #")
Else

    List.Items.Clear()
    Number.Text = ""
    Dim i As Integer = Lower.Text

    Do While i <= System.Convert.ToInt32(Upper.Text) 'Loop generates the numbers between values specified
        List.Items.Add(i)
        i += 1
    Loop

    'Select a random value from the list generated
    Dim myRandom As New Random
    Dim b As Integer = List.Items.Count
    Dim chosenItem As System.Object = List.Items.Item(myRandom.Next(b))
    Number.Text = chosenItem.ToString

End If

Upvotes: 1

Related Questions