Solved

C# Lists Remove, Add keep track of indexes

Posted on 2010-09-19
2
498 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

13 Experts available now in Live!

Get 1:1 Help Now