tags:

views:

84

answers:

4

have your say your idea or correct the example below please.

List<string> optionList = new List<string>
{
"AdditionalCardPersonAdressType","AdditionalCardPersonSex","AutomaticRaiseCreditLimit","CardDeliveryTimeWeekDay","CardDeliveryTimeWeekEnd","CCAdditionalCardAddress",
"CCAdditionalCardPerson","CCAutoPaidType","CCSendAddressType","CCStatementAddressType","CCStatementAddressType","CCStatementLanguage","CCStatementPeriod",
"CCStatementType","CompanyOwner","CustomerType","EducationStatus","ExchangeAccount","HomeStatus","IsCCAutoPaid","LogoName",
"MaritalStatus","PensionFund","RelationToCardHolder","Sex"}();
+5  A: 

Just remove () at the end.

List<string> optionList = new List<string>
            { "AdditionalCardPersonAdressType", /* rest of elements */ };
Padel
@Oded: http://msdn.microsoft.com/en-us/library/bb384062.aspx "Collection initializers"
Lucero
Are you referring to a specific version of .NET/C#? As far as I know this work from v3.5 and later. Don't know about 2.0 because I haven't used it for a while...
Padel
asp.net 2.0 btw I get the error after { -> Error 7 A new expression requires () or [] after type
blgnklc
In 2.0 you must use it like this: `List<string> optionList = new List<string>() { "AdditionalCardPersonAdressType", /* rest of elements */ };`. Note the `()` here: `new List<string>()`.
Padel
Though it seems they are not mandatory either...
Padel
It's a C# 3 language feature and has nothing to do with the version of the framework you're using. If you're still using Visual Studio 2005 you're not going to be able to use this feature.
Phil
A: 

You could get the values from an external configuration file, an XML file, or a database. The additional advantage is that you can change it without recompilation.

Sjoerd
A: 

You haven't really asked a question, but the code should be

List<string> optionList = new List<string> { "string1", "string2", ..., "stringN"}; 

i.e. no trailing () after the list.

Unsliced
Error 7 A new expression requires () or [] after type
blgnklc
@blgnklc, because you're using C# 2 which doesn't support this feature.
Phil
+2  A: 
List<string> mylist = new List<string>(new string[] { "blah, blah, blah" });

I shall refrain from being worried about some of the details in the OP's list... :)

Zenzer
Cunning use of the IEnumerable parameter. I like it.
Phil