VB.Net - For Loop error: Undefined Var
VB.Net - For Loop error: Undefined Var
For i = 0 To R.Tables(0).Rows.Count - 1
' do stuff
Next
与 vb.net 合作。它说变量 'i' 没有声明。任何解决方案?已经检查过我是否在任何地方声明了另一个 'i' 变量。如果我用其他东西改变 'i' 它也会做同样的事情。
根据 MSDN,如果您之前没有声明 "i",您应该更改语法:
For index As Integer = 1 To 5
Debug.Write(index.ToString & " ")
Next
在你的情况下,应该是这样的:
For i As Integer = 0 To R.Tables(0).Rows.Count - 1
'do stuff
Next
您也可以使用 for each,即:
For Each row As DataRow In R.Tables(0).Rows
'do stuff using row
Next
编辑
如 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 Each Row As DataRow In R.Tables(0).Rows
'Do Stuff
Next
For i = 0 To R.Tables(0).Rows.Count - 1
' do stuff
Next
与 vb.net 合作。它说变量 'i' 没有声明。任何解决方案?已经检查过我是否在任何地方声明了另一个 'i' 变量。如果我用其他东西改变 'i' 它也会做同样的事情。
根据 MSDN,如果您之前没有声明 "i",您应该更改语法:
For index As Integer = 1 To 5
Debug.Write(index.ToString & " ")
Next
在你的情况下,应该是这样的:
For i As Integer = 0 To R.Tables(0).Rows.Count - 1
'do stuff
Next
您也可以使用 for each,即:
For Each row As DataRow In R.Tables(0).Rows
'do stuff using row
Next
编辑
如 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 Each Row As DataRow In R.Tables(0).Rows
'Do Stuff
Next