Phil Marsh - 1 year ago 83
Vb.net Question

# Increase or decrease values over time

Having a bit of a brain fart to be honest, so I do apologize if this is a simple question with a simple answer!

I am trying to create an integer that will increase by value by + (and if possible, minus (so that it can fluctuate naturally)) over time through a timer. This is my current code:

``````If timer_temp_int = 0 Then
timer_temp.Enabled = False
lblRemaining.Text = "0"
MsgBox("Please check the temperature of the heater")
Else
'PROBLEM HERE
Dim temp2 As Decimal
temp2 = temp += 0.01
txt_temp.Text = temp
timer_temp_int -= 1
End If
``````

So, what I did was I assigned a second variable so that the temperature can be increased (this btw, is pulled from a random array)

``````Dim rn As New List(Of Integer)
Dim temp As Integer
Dim rnd As New Random
Dim randomValue As Integer = rn(rnd.Next(0, 5))
temp = randomValue.ToString()
``````

``````Dim temp2 As Decimal
temp2 = temp += 0.01
txt_temp.Text = temp
``````

but this appears to be the issue, as I get: "end of statement expected".
Would there be a way of getting this to perhaps fluctuate (randomly, if possible though this isn't a worry) by +/- 0.01 for every second of the 60 second timer. The reason for this is I am creating a temperature application as an assignment, so I need the temperature to fluctuate by +/- 0.01 as the application runs to simulate natural temperature fluctuation.

For the originally posted question, the error message is the result of:

``````temp2 = temp += 0.01
``````

This is trying to combine 2 statements:

``````temp += .01
temp2 = temp
``````

For the larger question, this code should allow each initial value to fluctuate. I dont know if each is supposed to do so independently, so you might have to change that:

``````' form level vars:
Private RNG As Random()
Private currVals As Decimal() = {80D, 90D, 100D, 110D, 120D}
Private TBs As TextBox()
``````

Then initialize the TB array in form load:

``````TBs = {TextBoxA, TextBoxB, TextBoxC, TextBoxD, TextBoxE}
RNG = New Random()
``````

Now you can fluctuate the base values in a loop which can be fired from a timer tick or whatever:

``````For n As Int32 = 0 To currVals.Length - 1
' next bool is an extension; same as RNG.Next(0,2)
If RNG.NextBoolean() Then
currVals(n) += 0.01D
Else
currVals(n) -= 0.01D
End If
TBs(n).Text = currVals(n).ToString()  ' update display
Next
``````

UI Controls are horrible variables, especially in this case. The text controls contain string, you are working with decimals. So the code changes the array value and updates the control (which seems like what you are after).

If the change is supposed to be the same for all the values, just pick one Boolean outside the loop. `NextBoolean` is an extension method, the long form would be:

``````Dim thisVal = RNG.Next(0, 2)   ' pick from 0 to 1
If thisVal = 0 Then