Solved

C# Lists Remove, Add keep track of indexes

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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