Making Global Variable from for loop???

Sabour

Active member
Joined
Sep 1, 2006
Messages
25
Programming Experience
Beginner
how to use set variable after the for loop?

i mean something like this
VB.NET:
For ....
Variable C
Next
Variable C ' if set variable C to display the value always zero :(
 
sorry man :D

For ....
'End Of For
Variable C = "A" 'Example,Variable C always Dynamic
Next
Variable C ' In here variable C value is zero
how to use Same Value the VariableC in End of For Loop to be used in Next phase
 
If you mean that you want the value of the C variable to have the same value after the loop as it did in the loop then it does. Once the loop is complete the variable will contain the last value assigned to it in the loop. Are you saying that you actually want to keep all the values assigned to the variable in the loop? If so then you can't use a single variable. One variable cannot contain multiple values, so you'd have to use an array or collection instead, so that it can contain all the values from the loop.
VB.NET:
Dim var As Integer = 0

MessageBox.Show(var.ToString()) 'Shows "0".

For i As Integer = 1 To 10
    var = i
    MessageBox.Show(var.ToString())
Next i

MessageBox.Show(var.ToString()) 'Shows "10".
VB.NET:
Dim arr(10 - 1) As Integer

For i As Integer = 0 To arr.GetUpperBound(0) Step 1
    arr(i) = i * 100
Next i

'arr now contains all the values assigned in the loop.

For Each var As Integer In arr
    MessageBox.Show(var.ToString())
Next var
 
Back
Top