如何避免后期绑定?
How do I avoid Late Binding?
For Each drCurrent In chatID.Rows
LastChatIDCheck = drCurrent("mid")
Next
drCurrent("mid")
显然是 VB.net 中的后期绑定。我该如何避免这种情况?
不确定是否可以避免后期绑定问题,但可以使用 DataRow 的字段扩展 class。
使用 Field 扩展,返回的值是强类型的,至少在代码级别,您没有将结果存储在对象类型中。
Dim LastChatIDCheck As Integer
For Each drCurrent As DataRow In chatID.Rows
LastChatIDCheck = drCurrent.Field(Of Integer)("mid")
Console.WriteLine(LastChatIDCheck)
Next
此外,一个适用于任何地方的好习惯是在程序的编译器选项中打开 Option Strict
For Each drCurrent In chatID.Rows
LastChatIDCheck = drCurrent("mid")
Next
drCurrent("mid")
显然是 VB.net 中的后期绑定。我该如何避免这种情况?
不确定是否可以避免后期绑定问题,但可以使用 DataRow 的字段扩展 class。
使用 Field 扩展,返回的值是强类型的,至少在代码级别,您没有将结果存储在对象类型中。
Dim LastChatIDCheck As Integer
For Each drCurrent As DataRow In chatID.Rows
LastChatIDCheck = drCurrent.Field(Of Integer)("mid")
Console.WriteLine(LastChatIDCheck)
Next
此外,一个适用于任何地方的好习惯是在程序的编译器选项中打开 Option Strict