C# Visual Basic中用于每个循环的var的等效值
我需要在Visual Basic中运行一些与C#中的代码等效的代码: 我知道在VB中最接近于声明“var”的是C# Visual Basic中用于每个循环的var的等效值,c#,vb.net,C#,Vb.net,我需要在Visual Basic中运行一些与C#中的代码等效的代码: 我知道在VB中最接近于声明“var”的是 Dim something = 但是,在foreach循环中如何实现这一点 您只需使用: For Each item In removeRows two.ImportRow(item) Next VB中的作为数据类型规范是可选的。有关详细信息,请参阅文档。中的每个文档都写有 所以 省略VB.NET(VB9)中的类型将隐式键入变量。您可以尝试(使用对象),类型是可选的 For
Dim something =
但是,在foreach循环中如何实现这一点 您只需使用:
For Each item In removeRows
two.ImportRow(item)
Next
VB中的
作为数据类型
规范是可选的。有关详细信息,请参阅文档。中的每个文档都写有
所以
省略VB.NET(VB9)中的类型将隐式键入变量。您可以尝试(使用对象),类型是可选的
For Each item As Object In removeRows
two.ImportRow(item)
Next
大概是这样的:
Dim siteName As String
Dim singleChar As Char
siteName = "HTTP://NET-INFORMATIONS.COM"
For Each singlechar In siteName
two.ImportRow(singleChar);
Next
您不需要vb中的“as”:
正如其他人所提到的,如果启用了“推断”选项,则使用“类型为”选项。我怀疑您的项目关闭了“推断”选项(这是导入在.Net 2.0中启动的现有项目时的默认选项)。在项目文件顶部或项目的编译设置中启用“推断”选项
Option Infer On
'' This works:
For Each item In removeRows
two.ImportRow(item)
Next
Option Infer Off
'' Requires:
For Each item As DataRow In removeRows
'' I'm assuming the strong type here. Object will work with Option Strict Off
two.ImportRow(item)
Next
启用“推断”选项后,您可以取消“作为…”选项,然后将推断类型。
使用“推断”选项,如果您不使用类型,将假定“对象”类型。仅供参考Dim something
实际上与var
不同-更像是在Dim
语句中将作为数据类型
,更像var
。对于每个
,这与是一样的-更多的是不包括说明符的问题。这样做会导致错误“'item'未声明。由于其保护级别”@NealR-hrm。。。使用上面的代码,您不应该得到这样的结果。“removeRows”是如何声明/定义的?@NealR您使用的是什么版本?我们使用的是.net 4.0。将“项声明为对象”解决了问题。@NealR尝试启用选项推断
,然后您可以将忽略为对象
For Each item As Object In removeRows
two.ImportRow(item)
Next
Dim siteName As String
Dim singleChar As Char
siteName = "HTTP://NET-INFORMATIONS.COM"
For Each singlechar In siteName
two.ImportRow(singleChar);
Next
For Each p In Process.GetProcesses()
Debug.WriteLine(p.ProcessName)
Next
Option Infer On
'' This works:
For Each item In removeRows
two.ImportRow(item)
Next
Option Infer Off
'' Requires:
For Each item As DataRow In removeRows
'' I'm assuming the strong type here. Object will work with Option Strict Off
two.ImportRow(item)
Next