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?

1
  • if you want to remove then you need list not array Commented Jul 12, 2015 at 5:37

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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();

3 Comments

this will create another array. but its simpler :)
@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.
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 :)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.