Federico Pessina Federico Pessina - 21 days ago 5
Vb.net Question

VB.Net - For Loop error: Undefined Var

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


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.

Answer

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 & " ")
Next

In your case, should be something like this:

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

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

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

Edit

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.