0

I have the following code in C#:

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

It's working fine. However, when I converted it to 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

I get the following compile-time error:

BC30205: End of statement expected.

What's the problem with the code?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
satish
  • 41
  • 5

4 Answers4

3

My psychic debugging skills tell me that you're using VB.Net 2005, which does not support anonymous types.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0
lista.Add(New InvValorMedio With {.Data_Base = _dataBase, _
                                  .Tipo = item.IdInvTipo, _
                                  .Valor = 0})
0

Its possible that this is a version of VB.Net prior to 2010, in which case the FROM syntax isn't available (I had the same issue earlier with some code converted by developerfusion - I was going from C# in .Net 4 to VB.Net in .Net 3.5)

The following 2 stage process should do it - not found a way of making it a single line yet:

Dim arr() = { _
    New With {.Value = 1, .Display = "ap"}, _
    New With {.Value = 2, .Display = "up"} _
}
return = New ArrayList(arr)
Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
  • 1
    Correction: According to comments this is in .Net 2, in which case the above anonymous types aren't available. However I'm leaving the answer up for reference for other users having problems with `FROM` – Jon Egerton Dec 19 '11 at 18:58
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
competent_tech
  • 44,465
  • 11
  • 90
  • 113