Federico Pessina Federico Pessina - 3 months ago 26
Vb.net Question

VB.Net - For Loop error: Undefined Var

For i = 0 To R.Tables(0).Rows.Count - 1
' do stuff

Working with vb.net. It says that variable 'i' is not declared. Any solution? already checked if i have declare another 'i' var anywhere. it does the same also if i change 'i' with something else.


According to MSDN you should change your syntax in case you had not declared "i" previously:

For index As Integer = 1 To 5
    Debug.Write(index.ToString & " ")

In your case, should be something like this:

For i As Integer = 0 To R.Tables(0).Rows.Count - 1
    'do stuff

You could also work with for each, which would be:

For Each row As DataRow In R.Tables(0).Rows 
    'do stuff using row


As Konrad Rudolph said below, you could also turn Option Infer On at the beginning of your class and leave your code as it is. It would let the compiler define the type of the variable. You can use the MSDN for more info.

Best regards.