0

Background on the program: the heart of the program needs to track all other Forms or other elements of the program. To do this, I am using a Form array

What I am wanting to do is take my initially defined array containing 1 element, and increase the length of the array to add a new element whenever a new Form is launched.The trouble I've run into is that I can't find out how to do this without using a second array to store the old array, re-declaring the original array with array.length += 1, looping through to re-add the original content from the second array, then adding the new element. It's heavy and inconvenient since I need to use similar processes elsewhere.

The code I have, which works but is ugly, is this:

public class PCB
{
    Form[] Runners;
    public PCB()
    {
        Runners = new Form[1];
        Runners[0] = new GUI;
 .
 .
 .
 .
 .

void NewForm(Form app)
{
    Form[] TEMP = Runners;   //Create my new array equal to the first
    Runners = new Form[Runners.Length + 1];  //re-create the old array, with an additional element
    for (int k = 0; k < TEMP.Length; k++)
    {
        //add the original elements back to the original array
        Runners[k] = TEMP[k];
    }
    Runners[TEMP.Length] = App; //add the final element to the array

I hate having to use the loop-structure, as I feel that it can be done cleaner. what I'm looking for is a function similar to the ListBox.Items.Add([[ITEM]]), but for an array.

Does such a function exist, or do I need to continue with my ugly loops?

4 Answers4

4

Use a list instead:

List<Form> Runners = new List<Form>();

...

Runners.Add(app);

If you for some odd reason absolutely must use an array, then Array.Resize is what you're looking for, but a list is much better.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • +1, and to note, if there is performance concerns regarding `List` implementation, you can initialize with a starting size. – jdphenix Mar 15 '14 at 16:08
  • This looks perfect. Follow up questions though: If I wanted to call a function that I built in my Form, how would I reference the listed item in such a way to be able to call that function? Or should I post another question for that? – jluetzenberg Mar 15 '14 at 16:15
  • Found what I was looking for. 'Form gui = RunningCore[0]; gui.[function];' – jluetzenberg Mar 15 '14 at 16:55
4

I believe what you're looking for is a List. A List already does this for you under the hood in .NET. You can do this:

    List<Form> Runners;
    Runners = new List<Form>();

Now when you add to the list you can do:

    Runners.Add(new MyForm());

When you want to remove from the list you can do:

    Runners.Remove(MyForm);
The Mahahaj
  • 696
  • 6
  • 8
2

Just use a List<Form> list instead of arrays, which can be accessed just like an array and you can dynamically add other elements (list.Add(new Form(...))).

It is much more performant than an array and it has other features that can simplify your coding.

Saverio Terracciano
  • 3,885
  • 1
  • 30
  • 42
1

One way is to use Array.Resize P.S. !!! be sure that the references to your array won't be changed. Here is an example: using System;

class aaa
{
    static void Main()
    {
        string[] array = new string[10];
        Array.Resize(ref array, 20);

    }
}
Sargis Koshkaryan
  • 1,012
  • 3
  • 9
  • 19