0

I am trying to increment a number in my function, and i need the values updated in every tick

My Function is working in a timer

I tried this but it's giving me an error

Thank you for your help

The code i Tried:

My Public Class Form

Public Class Form1
    
    Dim i0 As Integer = 0
    Dim i1 As Integer = 0
    Dim i2 As Integer = 0
    Dim i3 As Integer = 0
    Dim i4 As Integer = 0
    Dim i5 As Integer = 0
    Dim i6 As Integer = 0
    Dim i7 As Integer = 0
    Dim i8 As Integer = 0
    Dim i9 As Integer = 0
    Dim i10 As Integer = 0

My Function


 Public Function IncrNum(ByRef i0 As Integer, ByRef i1 As Integer, ByRef i2 As Integer, ByRef i3 As Integer, ByRef i4 As Integer, ByRef i5 As Integer, ByRef i6 As Integer, ByRef i7 As Integer, ByRef i8 As Integer, ByRef i9 As Integer, ByRef i10 As Integer) As Integer

  i0 = i0 + 1
        If i0 = 100 Then i1 = i1 + 1 : i0 = 0
        If i1 = 100 Then i2 = i2 + 1 : i1 = 0 : i0 = 0
        If i2 = 100 Then i3 = i3 + 1 : i2 = 0 : i0 = 0 : i1 = 0
        If i3 = 100 Then i4 = i4 + 1 : i3 = 0 : i0 = 0 : i1 = 0 : i2 = 0
        If i4 = 100 Then i5 = i5 + 1 : i4 = 0 : i0 = 0 : i1 = 0 : i2 = 0 : i3 = 0
        If i5 = 100 Then i6 = i6 + 1 : i5 = 0 : i0 = 0 : i1 = 0 : i2 = 0 : i3 = 0 : i4 = 0
        If i6 = 100 Then i7 = i7 + 1 : i6 = 0 : i0 = 0 : i1 = 0 : i2 = 0 : i3 = 0 : i4 = 0 : i5 = 0
        If i7 = 100 Then i8 = i8 + 1 : i7 = 0 : i0 = 0 : i1 = 0 : i2 = 0 : i3 = 0 : i4 = 0 : i5 = 0 : i6 = 0
        If i8 = 100 Then i9 = i9 + 1 : i8 = 0 : i0 = 0 : i1 = 0 : i2 = 0 : i3 = 0 : i4 = 0 : i5 = 0 : i6 = 0 : i7 = 0
        If i9 = 100 Then i10 = i10 + 1 : i9 = 0 : i0 = 0 : i1 = 0 : i2 = 0 : i3 = 0 : i4 = 0 : i5 = 0 : i6 = 0 : i7 = 0 : i8 = 0
        If i10 = 100 Then  i10 = 0 : i0 = 0 : i1 = 0 : i2 = 0 : i3 = 0 : i4 = 0 : i5 = 0 : i6 = 0 : i7 = 0 : i8 = 0 : i9 = 0

 Return i0 i1 i2 i3 i4 i5 i6 i7 i8 i9 i10
      
       End Function

Timer Tick

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
   
   TextBox1.Text = RichTextBox1.Lines(i0) & RichTextBox1.Lines(i1) & RichTextBox1.Lines(i2) & RichTextBox1.Lines(i3) & RichTextBox1.Lines(i4) & RichTextBox1.Lines(i5) & RichTextBox1.Lines(i6) & RichTextBox1.Lines(i7) & RichTextBox1.Lines(i8) & RichTextBox1.Lines(i9) & RichTextBox1.Lines(i10)

IncrPost(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10)
End Sub

In every tick i want my TextBox1 update the Text

3
  • 1
    You want an array, not only to return the values but also to deduplicate your code and simplify the function a lot. Commented Jun 29, 2020 at 14:40
  • However, your existing code should work too (except maybe for i11 which you use without defining it), just don't return anything. You already pass your variables by reference anyway! Commented Jun 29, 2020 at 14:41
  • It looks like you don't want to increment a number in your function, you want to show lines of text from one TextBox into another. You may want to specify what's in the source textbox and what the destination textbox should contain when the Timer ticks. A sequence of 10 lines of text out of 100, repeating in a timed loop? Commented Jun 29, 2020 at 14:46

2 Answers 2

3

Any time you have a bunch of variables with incrementing names like that, what you probably want is an array or list of some kind. In this case a List(Of Integer) seems reasonable. Put your values into a list instead of individual variables:

Dim ints as New List(Of Integer) From { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

Then modify your function to accept/return a list:

Public Function IncrNum(ByRef ints As List(Of Integer)) As List(Of Integer)

You can access them by index similar to your current variable names:

ints(0) = ints(0) + 1
If ints(0) = 100 Then ints(1) = ints(1) + 1 : ints(0) = 0
' etc.

And then just return the list:

Return ints

As an aside, you may want to add some input checking at the start of the function to make sure the list has 11 elements. If it has fewer then accessing the index directly could result in an exception.

Sign up to request clarification or add additional context in comments.

1 Comment

It's working, that's great, thank you for your answer
0

I'm pretty sure you can use David's idea for a List(Of Integer) to hold the values, and then shorten your code quite a bit to the follwing:

Private ints As New List(Of Integer) From {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim sb As New System.Text.StringBuilder
    For Each i As Integer In ints
        If i < RichTextBox1.Lines.Count Then
            sb.Append(RichTextBox1.Lines(i))
        End If
    Next
    TextBox1.Text = sb.ToString()
    IncrNum()
End Sub

Private Sub IncrNum()
    ints(0) = ints(0) + 1
    For i As Integer = 0 To (ints.Count - 1)
        If ints(i) = 100 Then
            ints(i) = 0
            If i < (ints.Count - 2) Then
                ints(i + 1) = ints(i + 1) + 1
            End If
        End If
    Next
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.