0

我在 C# 中有以下代码:

public static ArrayList GetGenders()
{
    return new ArrayList()
    {
        new { Value = 1, Display = "ap" },
        new { Value = 2, Display = "up" }
    };
}

它工作正常。但是,当我将其转换为 VB.NET 时:

Public Shared Function GetGenders() As ArrayList
    Return New ArrayList() From { _
        New With { _
            .Value = 1, _
            .Display = "ap" _
        }, _
        New With { _
            .Value = 2, _
            .Display = "up" _
        } _
    }
End Function

我收到以下编译时错误:

BC30205:预期语句结束。

代码有什么问题?

4

4 回答 4

3

我的通灵调试技巧告诉我,您使用的是 VB.Net 2005,它不支持匿名类型

于 2011-12-19T18:43:52.693 回答
0

A VB2005-specific answer involves creating a class to hold the values, then populating the arraylist with instances of the class.

The class:

Public Class LookupList
    Private m_Value As Integer
    Private m_sDisplay As String

    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(ByVal wValue As Integer, ByVal sDisplay As String)
        Me.New()
        Me.Value = wValue
        Me.Display = sDisplay
    End Sub

    Public Property Value() As Integer
        Get
            Return m_Value
        End Get
        Set(ByVal value As Integer)
            m_Value = value
        End Set
    End Property
    Public Property Display() As String
        Get
            Return m_sDisplay
        End Get
        Set(ByVal value As String)
            m_sDisplay = value
        End Set
    End Property
End Class

And the method:

Public Shared Function GetGenders() As ArrayList
    Dim oList As New ArrayList
    oList.AddRange(New LookupList() {New LookupList(1, "ap"), New LookupList(2, "up")})
    Return oList
End Function

A solution that is slightly more inline with the original C# code is to create a collection class for the class:

Public Class LookupListCollection
    Inherits System.Collections.Generic.List(Of LookupList)

    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(ByVal ParamArray aItems As LookupList())
        Me.New()
        If aItems IsNot Nothing Then
            Me.AddRange(aItems)
        End If
    End Sub

End Class

which can then be called as:

Public Shared Function GetGenders() As LookupListCollection
    Return New LookupListCollection(New LookupList(1, "ap"), New LookupList(2, "up"))
End Function
于 2011-12-19T19:47:39.867 回答
0

这可能是 2010 年之前的 VB.Net 版本,在这种情况下,FROM语法不可用(我之前遇到了同样的问题,有些代码由 developerfusion 转换 - 我从 .Net 4 中的 C# 转到 VB。 .Net 3.5 中的网络)

以下 2 阶段过程应该做到这一点 - 尚未找到使其成为单行的方法:

Dim arr() = { _
    New With {.Value = 1, .Display = "ap"}, _
    New With {.Value = 2, .Display = "up"} _
}
return = New ArrayList(arr)
于 2011-12-19T18:55:32.883 回答
0
lista.Add(New InvValorMedio With {.Data_Base = _dataBase, _
                                  .Tipo = item.IdInvTipo, _
                                  .Valor = 0})
于 2014-01-28T20:18:43.853 回答