Federico Pessina Federico Pessina - 1 year ago 88
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download