Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a list of Anonymous Type in VB

I'd like to create a list of an anonymous type, for example:

Dim lineItem = New With {.Name = myFile(index).Last_Name & ", " & myFile(index).First_Name, _
                         .StartDate = myFile(index).Day,
                         .EndDate = myFile(index).Day}

I have created that anonymous type. Now I'd like to add it to a list of that type. How do I declare a list of that type?

like image 395
Daniel Avatar asked Jun 23 '09 17:06

Daniel


People also ask

Which of the following is used to create anonymous type?

We can create anonymous types by using “new” keyword together with the object initializer. As you can see from the below code sample, the type is store in a var and has two data items.

How do you define anonymous type?

Essentially an anonymous type is a reference type and can be defined using the var keyword. You can have one or more properties in an anonymous type but all of them are read-only. In contrast to a C# class, an anonymous type cannot have a field or a method — it can only have properties.

How do I make an anonymous object?

You create an anonymous type using the new operator with an object initializer syntax. The implicitly typed variable- var is used to hold the reference of anonymous types. The following example demonstrates creating an anonymous type variable student that contains three properties named Id , FirstName , and LastName .


4 Answers

Here's a handy method for creating a list of an anonymous type from a single anonymous type.

Public Function CreateListFromSingle(Of T)(ByVal p1 As T) As List(Of T)
  Dim list As New List(Of T)
  list.Add(p1)
  return List
End Function

Now you can just do the following

Dim list = CreateListFromSingle(dsResource)

EDIT OP wanted a way to create the list before creating an element.

There are 2 ways to do this. You can use the following code to create an empty list. It borders on hacky because you are passing parameters you don't ever intend to use but it works.

  Public Function CreateEmptyList(Of T)(ByVal unused As T) As List(Of T)
    Return New List(Of T)()
  End Function

  Dim x = CreateEmptyList(New With { .Name = String.Empty, .ID = 42 })
like image 64
JaredPar Avatar answered Sep 28 '22 18:09

JaredPar


Here is how to do it inline using casting by example (without creating a second function):

Sub Main()
    Dim x = New With {.Name = "Bob", .Number = 8675309}
    Dim xList = {x}.ToList()
End Sub

(based on c# version posted here )

Essentially you create the anonymous type, put it in an array ( {x} ) then use the array's .ToList() method to get a list.

like image 45
CleverPatrick Avatar answered Sep 28 '22 20:09

CleverPatrick


How about a single line construct like this?

Dim yourList = {(New With {.Name="", .Age=0})}.Take(0).ToList
like image 38
iQueue Avatar answered Sep 28 '22 18:09

iQueue


Since the type is anonymous, you must use generic and type-inference.

The best way is to introduce a generic function that creates an empty collection from a prototype object.

Module Module1

    Sub Main()
        Dim dsResource = New With {.Name = "Foo"}

        Dim List = dsResource.CreateTypedList
    End Sub

    <System.Runtime.CompilerServices.Extension()> _
    Function CreateTypedList(Of T)(ByVal Prototype As T) As List(Of T)
        Return New List(Of T)()
    End Function

End Module
like image 45
Dario Avatar answered Sep 28 '22 18:09

Dario