473,379 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Help with generic list

I need help with translating this:

This works i C#
List<Customercustomers = new List<Customer{

new Customer { Fullname = "Pelle" },

new Customer { Fullname = "Oskar" },

new Customer { Fullname = "Hasse" },

new Customer { Fullname = "Zrjan" },

new Customer { Fullname = "Harald" },

new Customer { Fullname = "Adam" }};
This does not work in VB:

Dim customers As New List(Of Customer) _

(New Customer With {.Fullname = "Pelle"}, _

New Customer With {.Fullname = "Oskar"}, _

New Customer With {.Fullname = "Hasse"}, _

New Customer With {.Fullname = "Zrjan"}, _

New Customer With {.Fullname = "Harald"}, _

New Customer With {.Fullname = "Adam"})

Can someone please tell me why?
/k
Oct 15 '08 #1
1 943
"kurt sune" <ap*@apa.comschrieb
>I need help with translating this:

This works i C#
List<Customercustomers = new List<Customer{

new Customer { Fullname = "Pelle" },

new Customer { Fullname = "Oskar" },

new Customer { Fullname = "Hasse" },

new Customer { Fullname = "Zrjan" },

new Customer { Fullname = "Harald" },

new Customer { Fullname = "Adam" }};
This does not work in VB:

Dim customers As New List(Of Customer) _

(New Customer With {.Fullname = "Pelle"}, _

New Customer With {.Fullname = "Oskar"}, _

New Customer With {.Fullname = "Hasse"}, _

New Customer With {.Fullname = "Zrjan"}, _

New Customer With {.Fullname = "Harald"}, _

New Customer With {.Fullname = "Adam"})

Can someone please tell me why?

Dim Customers As New List(Of Customer)( _
New Customer() { _
New Customer With {.Fullname = "Pelle"}, _
New Customer With {.Fullname = "Oskar"} _
} _
)

In C#, the {...} is called a "Collection Initializer" (see 7.5.10.3 C#
language spec) which does not exist (AFAIK) in VB.Net in exact the same way.
So you must help yourself by creating an array. However, the 1:1 VB
translation would not make use of an array but...:

dim tmp1, tmp2 as customer
tmp1 = new Customer With {.Fullname = "Pelle"}
customers.add(tmp1)
tmp2 = new Customer With {.Fullname = "Oskar"}
customers.add(tmp2)

That's what the IL code of the C# version produces.

See also:
http://www.eggheadcafe.com/software/...alizer-in.aspx
Armin

Oct 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Robert | last post by:
Where can i find a free web- based VC++ course? (also i've to dowanload it) I'm interested also in VB and C++ thanks, Robert
3
by: Michael Rockwell | last post by:
I am new to using C# generics and I am liking what I am finding. However the examples in online help are lacking. Can someone help me with the FindAll method of the generic List class? As I...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
0
by: Miguel Dias Moura | last post by:
Hello, I am saving a complex type in Asp.Net 2.0 profile. I am using an SQL 2005 database as a profile provider. I have been consulting MSDN as help to create my class. (Please, see my code...
0
by: Wiktor Zychla [C# MVP] | last post by:
We do have generic classes, methods and delegates. My question is: what reason prevents us from having generic properties and indexers? // impossible public List<T> GetList<T> { get { ... }
1
by: shapper | last post by:
Hello, I created a user control (.ascx) with a property as follows: Private _Messages As Generic.List(Of String) Public Property Messages() As Generic.List(Of String) Get Return _Messages...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
2
by: Fabrizio Romano | last post by:
Hello, I have a problem with a generic method. I have written a sieve to generate prime numbers. This method takes an input parameter which is the upperbound of the last prime I need to get. So...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.