GrayFullBuster
GrayFullBuster

Reputation: 1063

Is it possible to remain previous values from a List in C#

Here is my sample code:

private void button1_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        foreach (var i in numbers())
        {
            listBox1.Items.Add(i);
        }
    }

    private List<int> numbers()
    {
        List<int> counts = new List<int>();
        for (int i = 1; i <= 5; i++)
        {
            counts.Add(i);
        }
        return counts;
    }

I want to know if it is possible to not overwrite and remain the previous values from a List<>. In my sample if I click the button it will populate 1,2,3,4,5 in the listbox and when I click the button again I am expecting an output of 1,2,3,4,5,1,2,3,4,5 this values is what I am expecting from my List<> How could I possibly do that? BTW, the listbox there is just for display purposes. Thanks!

Upvotes: 0

Views: 150

Answers (4)

Tim Schmelter
Tim Schmelter

Reputation: 460238

You just have to remove the listBox1.Items.Clear(); therefore.

Apart from that, you can use Enumerable.Concat to append items:

var num1 = numbers();
var num2 = numbers();
foreach(var num in num1.Concat(num2))
{
    //  ...
}

or maybe using Enumerable.Range from the start, assuming you always want to add the same 5 items:

int currentCount = listBox1.Items.Count;
int groupSize = 5;
var allItems = Enumerable.Range(0, currentCount + groupSize)
                         .Select(i => 1 + i % groupSize);
foreach(var item in allItems)
{
    // ...
}

Upvotes: 3

O. R. Mapper
O. R. Mapper

Reputation: 20760

With List<int> counts = new List<int>();, you create a new, empty list. This happens anew with each call to numbers().

If I understand your question correctly, you want numbers() to add the five numbers to an existing lists, that hence grows with each call to numbers().

In order to achieve this, do not create a new List<int> with each call of numbers() that you return as a result value; instead create a private field for that list and modify it with each call to numbers():

private List<int> counts = new List<int>();

private List<int> numbers()
{
    for (int i = 1; i <= 5; i++)
    {
        counts.Add(i);
    }
    return counts;
}

Note that numbers() will still return the list, but it's not a new list; it's the same list instance every time.

Upvotes: 1

Mike
Mike

Reputation: 6239

Declare counts outside of the numbers() method, at class level. Every call to this method will then add 1,2,3,4,5 into the list.

private List<int> _counts = new List<int>();

private void button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    foreach (var i in numbers())
    {
        listBox1.Items.Add(i);
    }
}

private List<int> numbers()
{
    for (int i = 1; i <= 5; i++)
    {
        _counts.Add(i);
    }
    return counts;
}

Upvotes: 1

Alexei Levenkov
Alexei Levenkov

Reputation: 100555

Clear removes all items in the list. If you simple want to append all items from new list to the old list use AddRange:

 listBox1.Items.AddRange(numbers());

Upvotes: 1

Related Questions