ASP .Net 4.0 Remove Roles from List<string>

HI,

I'm trying to remove all the roles from a list that do not have a Master.SiteVariable in it.

here is my code

        List<string> roles2 = new List<string>();
        roles2.AddRange(Roles.GetAllRoles());

        if (HttpContext.Current.User.IsInRole("Grand_Puba"))
        {
        }
        else
        {
            roles2.Remove(Master.SiteVariable + "Grand_Puba");
        }

        //This does not work
        //If the role does not apply to this site, then remove it from the list.
        /*foreach (string item1 in roles2)
        {
            if (!item1.Contains(Master.SiteVariable))
           {
                roles2.Remove(item1);
                //break;
           }
        }
        */

Open in new window

atljarmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Use a reverse for loop instead of a foreach. You cannot modify a collection that you are iterating (i.e. foreach'ing) over.
0
atljarmanAuthor Commented:
Ok.  Can you provide an example based on what is provided above.  I'm sorry, this is the first time I've done anything like this.
0
käµfm³d 👽Commented:
for (int i = roles2.Length - 1; i > -1; i--)
{
    if (!roles2[i].Contains(Master.SiteVariable))
    {
        roles2.RemoveAt(i);
        //break;
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

atljarmanAuthor Commented:
Ok.  Getting an error on the page: System.Collections.Generic.List<string> does not contain a definition for 'Lenght' accepting a first argument of type 'System.Collections.Generic.List<string>' could be found (are you missing a using directive or assemply reference?)

I have System.Collections Imported and added System.Collections.Generic and the error is still showing.  Any thoughts.
0
käµfm³d 👽Commented:
Use Count instead of Length.
0
atljarmanAuthor Commented:
Kufmed, Thank you for the help.  I've been pulling my hair out over this one.   I don't fully understand your solution, but it worked and that is all that matters for now.  I will come back to it and review at a later date when I have more time.
0
käµfm³d 👽Commented:
Just know that any time you want to add or remove from a collection, you cannot do it within a foreach that is iterating over that same collection.

A for loop does not use an iterator (a special way of jumping from one item to the next). Even though in a logical sense you are "iterating" over a collection when you use a for loop, you are not doing so by way of an iterator--you are doing so using an index into the collection. This way of doing it is fine as long as you don't provide an invalid index. This is why I had you do the for loop in reverse.

Let's say you have a list of three items:

red, blue, green

If you were to for over that list, and remove items from that list, you would get an IndexOutOfRange execption. Here's why:

On the first iteration of the loop, you have 3 elements, and your starting index is 0. Removing the first element (at index 0, which is "red") will give you a list of now only 2 elements (max index 1). Now your loop variable goes to 1. Removing the item at index 1 will remove "green" because "blue" is now at index 0 after you removed "red". Now your loop variable goes to 2. There is only one element remaining in the list, so your maximum index is 0. 2 is greater than 0, so you get the exception.

If you go in reverse, this doesn't happen.

On the first iteration of the loop, you have 3 elements, and your starting index is 2. Removing the last element (at index 2, which is "green") will give you a list of only 2 elements (max index 1). Now your loop variable goes to 1. Removing the item at index 1 will remove "blue" because it is still at index 1--nothing caused it to shift indices in the previous removal of "green". Now your loop variable goes to 0. There is only one element ("red") remaining in the list, and it is at index 0. 0 is still a valid index into the collection, so "red" can be removed without incident.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.