String Array to List

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am trying to create an object where one of the properties is a list
of string. So I am creating an array of strings and converting it to a
list. Something like:

Account a = new Account { Approved = true, Roles = (new String[]
{ "Admin", "Col" }).ToList());

I am talking about the Roles which is a List<String>

I get the error:
Syntax error, ',' expected

What am I missing?

Can't I create this object in one code line?

Thanks,
Miguel
 
Hello,

I am trying to create an object where one of the properties is a list
of string. So I am creating an array of strings and converting it to a
list. Something like:

Account a = new Account { Approved = true, Roles = (new String[]
{ "Admin", "Col" }).ToList());

I am talking about the Roles which is a List<String>

I get the error:
Syntax error, ',' expected

What am I missing?

Can't I create this object in one code line?

Thanks,
Miguel

Ooops, I solved it. I was missing the } to close the Account object.

Thanks,
Miguel
 
You meant:

var foo = new List<String>() { "blah", "blither" };

It seems that the constant list of string does not qualify as
IEnumerable<String>, so


var foo = new List<String>( { "blah", "blither" } );

fails to compile.



Vanderghast, Access MVP
 
vanderghast said:
You meant:

var foo = new List<String>() { "blah", "blither" };

It seems that the constant list of string does not qualify as
IEnumerable<String>, so


var foo = new List<String>( { "blah", "blither" } );

fails to compile.

Just remove the parentheses:

var foo = new List<String> { "blah", "blither" };
 
Göran Andersson said:
Just remove the parentheses:

var foo = new List<String> { "blah", "blither" };

var foo = new List<String>( new String[] { "blah", "blither" } );

should also compile.

But it is most relevant if still at 2.0.

Arne
 
Back
Top