?
Solved

C# Lists Remove, Add keep track of indexes

Posted on 2010-09-19
2
Medium Priority
?
505 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

762 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