Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Lists Remove, Add keep track of indexes

Posted on 2010-09-19
2
Medium Priority
?
506 Views
Last Modified: 2012-05-10
Hello,

I have the below code - created a list and populated it with all true booleans as default.  When the filter isn't satisfied false is applied.  Current problem is keeping track of the indexes - found adding:

   goodOrBad.RemoveAt(i);
   goodOrBad.Add(false);

for the first index still returns true at goodOrBad[0].  Even through it entered the statement to convert it.  How are the indexes stored?  Are they in reverse?  How can this be solved?

 for (int l = 0; l <number; l++)
            {
                goodOrBad.Add(true);
            }

            foreach (KeyValuePair<string, List<FilterClass>> filter in d)
            {
                if (filter.Key.Equals(selectedFilterList[0]))
                {
                    List<FilterClass> list = filter.Value;

                    foreach (FilterClass c in list)
                    {
                        double lowerValue = c.LowerFilter;
                        double upperValue = c.UpperFilter;

                        double[] a = results.ValuesByName(c.Name);

                        // take index of failing add to set
                        for (int i = 0; i < a.Length; i++)
                        {


                           if (((double)a.GetValue(i) <= lowerValue) ||
                                ((double)a.GetValue((i)) >= upperValue))
                            {
                                if (goodOrBad[i].Equals(true))
                                {
                                
                                    goodOrBad.RemoveAt(i);
                                    goodOrBad.Add(false);
                                                                    }

                            }

                        }
                    }
                }
            }

Open in new window


Thanks
0
Comment
Question by:AndyC1000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
mahmoodvcs earned 2000 total points
ID: 33711158
Maybe I didn't understand your question well. but it seems you want change a value in the list from true to false.
instead of :
goodOrBad.RemoveAt(i);
goodOrBad.Add(false);

use :

goodOrBad[i] = false;

or:

goodOrBad.RemoveAt(i);
goodOrBad.Insert(i,false);





goodOrBad[i] = false;

or:

goodOrBad.RemoveAt(i);
goodOrBad.Insert(i,false);

Open in new window

0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 33711522
When you use goodOrBad.RemoveAt(i) that record is deleted from the list and everything above it shifts one index down.
If you use goodOrBad.Add(False), the value is added at the end of the list
Change to
  if (((double)a.GetValue(i) <= lowerValue) || ((double)a.GetValue((i)) >= upperValue))
  {
        goodOrBad[i] = false;
  }
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question