0

I have a class which contains an array of a second class just like this:

public class SimpayRecords
{
    public int a;
    public int b;
    public int c;
    public SimpayRecord[] records;
}

I have a third class which contains the SimpayRecords class. In this third class i want to loop through the array and remove unwanted items. Something like this:

for (int i = 0; i < this.Records.Records.Length; i++)
{
    if (this.Records.Records[i].Date < this.LastTime)
      //remove TempRecords.Records[i]
}

how can i do it?

Frank Boyne
  • 4,400
  • 23
  • 30
Mohammad
  • 2,724
  • 6
  • 29
  • 55

3 Answers3

1

Array elements cant be removed. you need List instead.

Make sure to reference System.Collections.Generic.

using System.Collections.Generic;

Your class would be like.

public class SimpayRecords
{
    public int a;
    public int b;
    public int c;
    public List<SimpayRecord> records; // This is the List.
}

To remove from list

for (int i = 0; i < this.Records.Records.Count; i++)
{
     if (this.Records.Records[i].Date < this.LastTime)
            this.Records.Records.RemoveAt(i--); // Removes the element at this index
}

Because List is Diffrent from normal array. so you have to learn about List. how to create them and how to work with them. so take a look at these.

https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx http://www.dotnetperls.com/list

Community
  • 1
  • 1
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
1

If reassigning a new array instance to this.Records.Records is not out of the question, you can do it with a simple WHERE condition (reversing your condition to a >=) in a LINQ query, like this:

using System.Linq;

// ...

this.Records.Records = this.Records.Records.Where(r => r.Date >= this.LastTime).ToArray();
sstan
  • 35,425
  • 6
  • 48
  • 66
  • this will create another array. but its simpler :) – M.kazem Akhgary Jul 12 '15 at 05:48
  • @M.kazem: the main advantage of simplicity is that it's easier to write bug free code. Especially when performing deletes in a loop, it's easy to get it wrong, because when the deletes happen, if you don't adjust the index to account for the shifting of elements, then you can inadvertently skip certain elements in the list. hint, hint. – sstan Jul 12 '15 at 05:57
  • yes i agree with simplicity.however i forgot to reduce the counter when removing from, (i just fixed it now.) . i think OP should first learn List. so he/she will become better programmer :) – M.kazem Akhgary Jul 12 '15 at 06:04
1

If you don't want to use List to store your data, you could use Extension method as shown in answer to this question

public static class ArrayExtensions{
     public static T[] RemoveAt<T>(this T[] source, int index)
     {
        T[] dest = new T[source.Length - 1];
        if( index > 0 )
            Array.Copy(source, 0, dest, 0, index);

        if( index < source.Length - 1 )
            Array.Copy(source, index + 1, dest, index, source.Length - index - 1);

        return dest;
     }
}

Then you can use RemoveAt method as following:

Foo[] bar = GetFoos();
bar = bar.RemoveAt(2);

Hope this helps

Community
  • 1
  • 1
Umriyaev
  • 1,150
  • 11
  • 17