将我的代码从C#转换为VB.NET,现在它引发了一个错误

时间:2012-03-04 17:42:49

标签: vb.net

有人可以向我解释这个错误的含义吗?我是VB编程的新手,不明白这一点。

  

错误:未定义类型'var'

这是我的代码

Dim result = From s In reader.GetSales() _
                 Join c In reader.GetSalesRep() _
                     On s.SalesRepID Equals c.SalesRepID _
             Order By c.SalesRepID _
             Select New _
                 With { _
                     .SalesRepID = c.SalesRepID, _
                     .FirstName = c.FirstName
                     ''# truncated for brevity
                 }

Dim sb As New StringBuilder(50)
Dim path As String = "..\debug\CarSalesFiles\Commissions" _
                   + DateTime.Today.Year _
                   + DateTime.Today.Month _
                   + DateTime.Today.Day _
                   + ".txt"

If Not File.Exists(path) Then
    Using sw As StreamWriter = File.CreateText(path)

        ''# Here's the error line
        For Each var In result
            sb.Append(item.SalesRepID + _seperator)
            sb.Append(item.LastName + _seperator)
            sb.Append(item.FirstName + _seperator)
        Next

4 个答案:

答案 0 :(得分:4)

C#关键字var在VB.NET中没有直接的等价物,而是需要设置 Option Infer On

或者,您可以明确指定类型:

For Each foo As Sale In result

答案 1 :(得分:0)

这就是我想出的。您的匿名类型未设置ToList(),因此您的循环无法按预期工作。您还在循环中使用var变量,然后使用item

查看我的调整。

' I did NOT test your LINQ query, I just added .ToList()
Dim results = (From s In reader.GetSales() _
                  Join c In reader.GetSalesRep() _
                  On s.SalesRepID Equals c.SalesRepID _
                  Order By c.SalesRepID _
                  Select New List(Of Results) With { _
                      .SalesRepID = c.SalesRepID, _
                      .FirstName = c.FirstName
                      ''# truncated for brevity
                  }).ToList() 

'...

If Not File.Exists(path) Then
    ''# not sure the purpose of the streamWriter here.
    ''# Using sw As StreamWriter = File.CreateText(path)

        For Each result In results ''# Notice the 'item' not 'var'
            sb.Append(result.SalesRepID + _seperator)
            ''# ...
        Next
End If

修改

在使用@cHao进行搜索时,如果代码中已启用Option Strict,则可能需要为匿名类型result制作POCO ...请参阅下文。

Public Class Result
    Private Property _SalesRepID As Integer
    Public Property SalesRepID As Integer
        Get
            return _SalesRepID
        End Get
        Set(ByVal Value As Integer)
            _SalesRepID = value
        End Set
    End Property

    ' Wash / Rinse / Repeat for ALL your properties
End Class

然后在您的示例代码中

' Again, I did NOT test your LINQ query, I just added .ToList()
Dim results As List(Of Result) = (From s In reader.GetSales() _
                                  Join c In reader.GetSalesRep() _
                                  On s.SalesRepID Equals c.SalesRepID _
                                  Order By c.SalesRepID _
                                  Select New Result With { _
                                     .SalesRepID = c.SalesRepID, _
                                     .FirstName = c.FirstName
                                     ''# truncated for brevity
                                  }).ToList()

' ....

For Each result As Result In results ''# Notice the 'result' not 'var'
    sb.Append(result.SalesRepID + _seperator)
    ''# ...
Next

' Apologies for minor mistakes, the SO editor isn't quite as awesome as Visual Studio.

答案 2 :(得分:0)

您似乎正在使用“var”但尚未定义它。注意:在VB中,变量是使用谓词DIM定义的。在使用变量之前,您需要声明变量。 VAR的C#构造在这里并不完全适用。但是,我自己并不是VB的强大用户,但请记住你能做到:

DIM var(创建var) DIM var as String(稍后声明的类型)

昏暗可以被视为“我正在声明一个变量,但无法告诉你它的行为方式。”

简单地说,在VB中,您可以在为变量分配类型或值之前创建变量。但是,我会指出你对DIM和VAR的差异做出更好的解释。

Scott Hanselman - Back to Basics: var != Dim

我假设您正在编写某种办公代码?使用VB时,C#通常应该同样好(如果不是更好)。在处理OFFICE集成应用程序时,文章中引用了一个例外。

希望这有帮助。

答案 3 :(得分:0)

试试这个:

For Each item In result

要使其正常工作,您必须将Option Infer设置为On。此外,您似乎正在使用stringbuilder作为最终写入文件的缓冲区。最好直接写入文件流。