473,385 Members | 1,647 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,385 software developers and data experts.

Add item at start

Hello,

I am creating a list of Role as follows:

IList<Roleroles = 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
Nov 5 '08 #1
6 1692
"shapper" <md*****@gmail.comwrote in message
news:73**********************************@w24g2000 prd.googlegroups.com...
After I add all roles I need to add a new role at the start of the
list.
Look at the Insert() method.
Nov 5 '08 #2
roles.Insert(0, new Role { ... });
--
Stanimir Stoyanov
http://stoyanoff.info

"shapper" <md*****@gmail.comwrote in message
news:73**********************************@w24g2000 prd.googlegroups.com...
Hello,

I am creating a list of Role as follows:

IList<Roleroles = 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
Nov 5 '08 #3
On Nov 5, 7:55*pm, "Jeff Johnson" <i....@enough.spamwrote:
"shapper" <mdmo...@gmail.comwrote in message

news:73**********************************@w24g2000 prd.googlegroups.com...
After I add all roles I need to add a new role at the start of the
list.

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

Nov 5 '08 #4
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

"shapper" <md*****@gmail.comwrote in message
news:44**********************************@v22g2000 pro.googlegroups.com...
On Nov 5, 7:55 pm, "Jeff Johnson" <i....@enough.spamwrote:
"shapper" <mdmo...@gmail.comwrote in message

news:73**********************************@w24g2000 prd.googlegroups.com...
After I add all roles I need to add a new role at the start of the
list.

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

Nov 5 '08 #5
On Nov 5, 8:54*pm, "Stanimir Stoyanov" <stoya...@REMOVETHIS.live.com>
wrote:
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

"shapper" <mdmo...@gmail.comwrote in message

news:44**********************************@v22g2000 pro.googlegroups.com...
On Nov 5, 7:55 pm, "Jeff Johnson" <i....@enough.spamwrote:
"shapper" <mdmo...@gmail.comwrote in message
news:73**********************************@w24g2000 prd.googlegroups.com....
After I add all roles I need to add a new role at the start of the
list.
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
Yes, I know ... but I was trying to do this in one code line ...
that's why I was trying this
Nov 5 '08 #6
On Wed, 05 Nov 2008 12:43:11 -0800, shapper <md*****@gmail.comwrote:
[...]
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?
Stanimir already explained the error. Personally, I think your goal of
doing it all in one line is at least a bit of an "over-reach", and may in
fact be contrary to nice, readable code. But, if you insist, you could do
this:

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

But honestly, I'd prefer the code Stanimir posted over the above. Just
because you _can_ obfuscate the code doesn't mean you should. :)

Pete
Nov 6 '08 #7

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

Similar topics

25
by: skull | last post by:
Hi everybody, it is my first post in this newsgroup. I am a newbie for python though I have several years development experience in c++. recently, I was stumped when I tried to del item of a list...
4
by: Kurt Schroeder | last post by:
I have a data grid with about 20 repeating items. they data: colors, red colors,blue colors, green Animals cat animals bird people, Tom people, Bill people, Kris
26
by: Simon Jefferies | last post by:
Hello, I am trying to add an item to a checked list box, like: clbList.Items.add("Hello",true) I get an error back: Run-time exception thrown: System.ArgumentOutOfRangeException -...
8
by: Joel Reinford | last post by:
I would like to build a class that has properties which can be accessed by string names or index numbers in the form of MyClass.Item("LastName"). The string names or item index values would be...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
16
by: sophie_newbie | last post by:
I have a list a little something like this: StringA StringC StringB StringA StringC StringD StringA ....
2
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
how do I code generic functions to return the next item in an enumeration a) sorted by name, b) sorted by value c) sorted by declaration in a round-robin style ? for example the enum is Enum...
1
by: chrisli | last post by:
Hey, i have written this code to read all Outlook Appointments from another user and fill them into my DGV. Public Sub ReadOtherUserAppointment(ByVal UserName As String) Dim objolApp...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.