I try to generate 10 random integers and store it in an array(9). Then I split the array into half by converting it into string. My code can compile but it sometimes work and most time cannot work. I don't know what is the problem....

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Randomize()
        Dim rand As New Random
        Dim array(9) As String
        Dim X As String
        Dim Y As String
        Dim i As Integer

        For i = 0 To 9
            array(i) = rand.Next
            i = i + 1
        Next

        Dim HiOrder As String = array(0).Substring(0, 5)
        Dim LoOrder As String = array(0).Substring(5, 5)

        X = 1.3 + (HiOrder * ((2.4 - 1.3) / ((10 ^ 5) - 1)))
        Y = 3.4 + (LoOrder * ((4.7 - 3.4) / ((10 ^ 5) - 1)))

        ListBox1.Items.Add(array(0))
        ListBox1.Items.Add(HiOrder)
        ListBox1.Items.Add(LoOrder)
        ListBox1.Items.Add("X = " & X)
        ListBox1.Items.Add("Y = " & Y)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

End Class

Can help me with this??? I can't seem to get it to run every time....

rand.Next

will create a random number between 0 and Int32.MaxValue

so if the random number is smaller than 9999 then "array(0).Substring(0, 5)" will fail
to make sure it dont fail do

rand.Next(10000,Int32.MaxValue)

First time might be u r getting array(0) is 10 digits and when click again the button, arrya(0) holding 9 digits. So when your are trying to use substring(5,5), length is not correct thats what throwing the exception.

Be a part of the DaniWeb community

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