15

I have been looking at some C# code:

List<Employee> Employees = new List<Employee>{
    new Employee{firstname="Aamir",lastname="Hasan",age=20},
    new Employee{firstname="awais",lastname="Hasan",age=50},
    new Employee{firstname="Bill",lastname="Hasan",age=70},
    new Employee{firstname="sobia",lastname="khan",age=80},  
    };

Now when I convert this to vb.net

Dim Employees as List(Of Employee) = New List(Of Employee)() With { New Employee() With { _  
.firstname = "Aamir", _  
.lastname = "Hasan", _   
.age = 20 _  
}, _  
New Employee() With { _  
.firstname = "awais", _  
.lastname = "Hasan", _  
.age = 50 _  
}, _  
New Employee() With { _  
.firstname = "Bill", _  
.lastname = "Hasan", _  
.age = 70 _  
}, _  
New Employee() With { _  
.firstname = "sobia", _  
.lastname = "khan", _  
.age = 80 _  
} _  
}  

I get the error "Name of field or property being initialized in an object initializer must start with'.'."

Now I can get an array of employee using the code:

Dim Employees = { New Employee() With { _  
.FirstName = "Aamir", _  
.LastName = "Hasan", _   
.Age = 20}, _  
New Employee() With { _    
.FirstName = "Awais", _   
.LastName = "Hasan", _  
.Age = 50}, _
New Employee() With { _
.FirstName = "Bill", _ 
.LastName = "Hasan", _  
.Age = 70 _
} _  
}    

But I would like a List(Of Employee) as it is bugging me as to why this doesnt work in vb.net?

Tim B James
  • 20,084
  • 4
  • 73
  • 103

3 Answers3

19

Collection initialisers were added in VB.NET 2010. This is air code, but here goes:

Dim Employees as List(Of Employee) = New List(Of Employee)() From
{ 
    New Employee() With { _   
       .firstname = "Aamir", _
       .lastname = "Hasan", _ 
       .age = 20 _   
    }, _
   New Employee() With { _  
       .firstname = "awais", _  
       .lastname = "Hasan", _ 
       .age = 50 _ 
    }, _ 
   New Employee() With { _ 
       .firstname = "Bill", _ 
       .lastname = "Hasan", _ 
       .age = 70 _ 
    }, _  
   New Employee() With { _ 
       .firstname = "sobia", _ 
       .lastname = "khan", _ 
       .age = 80 _ 
    } _ 
}   
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • 1
    VB2010 can also sometimes detect line continuations automatically, so it's possible the underscores are also unnecessary – MarkJ May 04 '12 at 13:36
  • I thought you always needed those? – Paul C Feb 01 '13 at 10:13
  • 1
    @codeblend In some cases, if you omit the line-continuation character, the Visual Basic compiler will implicitly continue the statement on the next line of code. [Documentation](http://msdn.microsoft.com/en-us/library/vstudio/ba9sxbw4.aspx) – MarkJ Feb 01 '13 at 14:55
  • 2
    Is there any performance difference between this and just doing `New List(Of Employee)({ ...new employee code here... })`? – ps2goat Jun 02 '14 at 18:38
  • @ps2goat I don't know, sorry. – MarkJ Jun 03 '14 at 12:16
18

EDIT (2)
As pointed out in comments, VB.NET collection initializers have now been introduced, and a lot of the following post should be considered obsolete.

EDIT
Don't always blindly trust the C# to VB.NET converter
Here's a handy tool for online conversion

Turns out VB.NET doesn't have collection initializers. Which means there is no equivalence of

var myList = new List<string>()
{
   "abc",
   "def"
};

... but it does have object initializers. So you can create an instance of a class and assign values to its properties all in one go, but you cannot create an instance of a list and add items to it all in one go.

There closest you can get is in the link above. You can create an Array and add items to it in a single operation, and then you have to ToList that array.

So this time I've actually compiled the code myself, and it works. Sorry for the hassle

    Dim EmployeesTemp As Employee() = { _
        New Employee() With { _
            .firstname = "Aamir", _
            .lastname = "Hasan", _
            .age = 20 _
        }, _
        New Employee() With { _
            .firstname = "awais", _
            .lastname = "Hasan", _
            .age = 50 _
        }, _
        New Employee() With { _
            .firstname = "Bill", _
            .lastname = "Hasan", _
            .age = 70 _
        }, _
        New Employee() With { _
            .firstname = "sobia", _
            .lastname = "khan", _
            .age = 80 _
        } _
    }

    Dim Employees as List(Of Employee) = EmployeesTemp.ToList()
David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • Sorry this still gives the same error. Name of field or property being initialized in an object initializer must start with'.'. I used that handy tool when converting it originally :) always use that. – Tim B James Jul 15 '10 at 21:04
  • 1
    @JimJams: hah! that had me doing some research alright. i'd say it's time you join the C# side. but hey, see my updated answer. – David Hedlund Jul 15 '10 at 22:11
  • 1
    Thanks David, this works a treat, and yes, it is time I joined the C# side! – Tim B James Jul 17 '10 at 15:54
  • 2
    Collection initialisers were added in [VB.Net 2010](http://msdn.microsoft.com/en-us/library/dd293617.aspx). The equivalent to your C# collection initialiser is `Dim myList = New List(Of String) From { "abc", "def" }` – MarkJ Jan 02 '11 at 02:38
  • It would be very good of you to put an edit to the top of your answer that collection initializers were added in 2010. Thanks :) – csauve Aug 28 '12 at 21:21
1

How about this?

Dim Employees As List(Of Employee) = { _
  New Employee() With { .firstname = "Aamir", .lastname = "Hasan", .age = 20 }, _
  New Employee() With { .firstname = "awais", .lastname = "Hasan", .age = 50 }, _
  New Employee() With { .firstname = "Bill",  .lastname = "Hasan", .age = 70 }, _
  New Employee() With { .firstname = "sobia", .lastname = "khan",  .age = 80 } _
}.ToList()
Eric
  • 2,207
  • 2
  • 16
  • 16