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?

6
  • What error do you get? What version of VB? Commented Dec 19, 2011 at 18:31
  • 2
    Define "it's not working." Do you get an error? Compile-time or run-time? Commented Dec 19, 2011 at 18:31
  • @JohnSaunders: Constructing a List<T> here is non-trivial. Commented Dec 19, 2011 at 18:40
  • My bad. Didn't notice the anonymous type. Commented Dec 19, 2011 at 18:41
  • i am using .net 2.0 framework Commented Dec 19, 2011 at 18:41

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

s iam using vb.net 2005.so no solution?
You can create a named class, or you can use a later version.
Or use a List(of ListItem).
@TimSchmelter: That won't help replace the anonymous type.
@Slaks: Not all anonymous types, but maybe this specific case where he needs a type with a property Value and a property Display. Why should he create a new class when it's already existing?
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)

1 Comment

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
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

Comments

0
lista.Add(New InvValorMedio With {.Data_Base = _dataBase, _
                                  .Tipo = item.IdInvTipo, _
                                  .Valor = 0})

1 Comment

If this is an answer, please add some description

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.