Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Visual Basic中用于每个循环的var的等效值_C#_Vb.net - Fatal编程技术网

C# Visual Basic中用于每个循环的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

我需要在Visual Basic中运行一些与C#中的代码等效的代码:

我知道在VB中最接近于声明“var”的是

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