Search code examples
vb.netdo-loops

Do Loop causes application freezing


I have to generate a sequence of number equally spaced one to another. I have the lower bound, the upper bound and the step from one to another. To this purpose I wrote a Do-Loop clause. If I try to do this with few numbers as output (the y-case in the provided code), it works fine, but when the numbers are more (the z-case in the code below), my software freezes. Here is the code I've provided in VS2019:

    Dim y As Double = DataGridView2.Rows(0).Cells(2).Value
    Dim z As Double = DataGridView2.Rows(0).Cells(4).Value
    Do
        ListBox8.Items.Add(y)
        y += CDbl(Form1.TextBox2.Text)
    Loop Until y = DataGridView2.Rows(0).Cells(3).Value
    ListBox8.Items.Add(y)
    Do
        ListBox9.Items.Add(z)
        z += CDbl(Form1.TextBox2.Text)
    Loop Until z = DataGridView2.Rows(0).Cells(5).Value
    ListBox9.Items.Add(z)

For the case I'm trying to make working, the y-case has 4 numbers as output, instead the z-case should provide 61 numbers as output. How can I solve this issue? Thanks all are gonna answer me.

Best regards


Solution

  • You could change it up to:

    While z <= DataGridView2.Rows(0).Cells(5).Value
        ListBox9.Items.Add(z)
        z += CDbl(Form1.TextBox2.Text)
    End While
    

    This way it can't get stuck in an infinite loop looking for an exact match.