Solved

C# Lists Remove, Add keep track of indexes

Posted on 2010-09-19
2
494 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
2 Comments
 
LVL 3

Accepted Solution

by:
mahmoodvcs earned 500 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:ewangoya
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now