Add item at start

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

shapper

Hello,

I am creating a list of Role as follows:

IList<Role> roles = new List<Role>();
roles.Add(new Role { Type = RoleType.Administrator, Name =
"Administrador", Open = false });
...

After I add all roles I need to add a new role at the start of the
list.

Can I do this?

Thanks,
Miguel
 
Look at the Insert() method.

Yes, I have tried it as follows:

MyRoles = Role.List(null, true).Insert(0, new Role { Name =
"Administrator", Open = true, Type = null });

Role.List returns an IList<Role>

But I get the following error:
Cannot implicitly convert type 'void' to
'System.Collections.Generic.IList<MyApp.Role>'

Any idea why this is happening?

Thanks,
Miguel
 
The correct lines of code are

MyRoles = Role.List(null, true);
MyRoles.Insert(0, new Role { Name = "Administrator", Open = true, Type =
null });

You were trying to assign the result of the Insert method (or the lack
thereof) to MyRoles. Instead, you should assign the result of your
Role.List() method to your List reference and then just call Insert() on the
new instance.
--
Stanimir Stoyanov
http://stoyanoff.info

Look at the Insert() method.

Yes, I have tried it as follows:

MyRoles = Role.List(null, true).Insert(0, new Role { Name =
"Administrator", Open = true, Type = null });

Role.List returns an IList<Role>

But I get the following error:
Cannot implicitly convert type 'void' to
'System.Collections.Generic.IList<MyApp.Role>'

Any idea why this is happening?

Thanks,
Miguel
 
The correct lines of code are

MyRoles = Role.List(null, true);
MyRoles.Insert(0, new Role { Name = "Administrator", Open = true, Type =
null });

You were trying to assign the result of the Insert method (or the lack
thereof) to MyRoles. Instead, you should assign the result of your
Role.List() method to your List reference and then just call Insert() on the
new instance.
--
Stanimir Stoyanovhttp://stoyanoff.info





Yes, I have tried it as follows:

MyRoles = Role.List(null, true).Insert(0, new Role { Name =
"Administrator", Open = true, Type = null });

Role.List returns an IList<Role>

But I get the following error:
Cannot implicitly convert type 'void' to
'System.Collections.Generic.IList<MyApp.Role>'

Any idea why this is happening?

Thanks,
Miguel

Yes, I know ... but I was trying to do this in one code line ...
that's why I was trying this
 
Back
Top