21

How do I add a new item from a TextBox and Button on a Windows Form at the end of an ArrayList which references a class?

private product[] value = new product[4];

value[1] = new product("One",5)
value[2] = new product("Two",3)
value[3] = new product("Three",8)

Workflow

  • Enter new products details into textbox1, textbox2, textbox3
  • When I click Add the new product gets added to the array:

    value[1] = new product("One",5)
    value[2] = new product("Two",3)
    value[3] = new product("Three",8)
    value[4] = new product("Four",2)

What is the code for doing this?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Timmy
  • 211
  • 1
  • 2
  • 3
  • Correction firstline should be: private product[] value = new product[4] Timmy – Timmy Dec 03 '09 at 00:57
  • I see that you posted the same comment in response to a few answers saying that you don't want to use a List. Is there any reason why? – rossisdead Dec 03 '09 at 02:48
  • Just beware that value[0] isn't initialized. It might be on purpose, though. – Steve Schnepp Dec 03 '09 at 16:26
  • 1
    `private product[] value = new product[4];` means you allocate place for 4 products. Arrays are zero-indexed in C#, so the first assignment line should be `value[0] = new product("One",5)` instead of 1. – Sune Rievers Dec 04 '09 at 00:35
  • @Timmy please don't post edits in the comments. You can edit your original post with the edit button. Thx – ShadowCrafter_01 Mar 17 '21 at 08:09

7 Answers7

37

Arrays are fixed size, which means you can't add more elements than the number allocated at creation time, if you need a auto sizing collection you could use List<T> or an ArrayList

Example:

// using collection initializers to add two products at creation time
List<Product> products = new List<Product>{new Product("One",5), new Product("Two",3) };

// then add more elements as needed
products.Add(new Product("Three",8));
harriyott
  • 10,505
  • 10
  • 64
  • 103
Pop Catalin
  • 61,751
  • 23
  • 87
  • 115
  • Ok guys thanks for all your answers....I do not want to try the List method...I will go with the Array resize...Would I use it the following way in the add button after entering details in textboxes: private void btnAdd_Click_1(object sender, EventArgs e) { Array.Resize(ref value, 5); } And how would I use the ArrayList? – Timmy Dec 03 '09 at 01:22
  • 1
    Timmy, you should use whatever makes more sense for you, but just so you know, `List` and `ArrayList` both use internally a private array that is resized using Array.Resize() automatically when adding or removing items. – Pop Catalin Dec 03 '09 at 01:44
  • Ok Sorry forgot to mention I do not want it to grow to big because its a test at the moment...So about 2 new products will do... will this work: private product[] value = new product[5]; value[0] = new product("One",5) value[1] = new product("Two",3) value[2] = new product("Three",8) or like this: private product[] value = new product[5]; value[0] = new product("One",5) value[1] = new product("Two",3) value[2] = new product("Three",8) value[3] = new product(textBox1,textBox2) value[4] = new product(textBox1,textBox2) – Timmy Dec 03 '09 at 01:51
20

Use List as other people mentioned. If you are set on arrays, use

Array.Resize<Product>(ref product, your new size);

If you're only going to be adding a couple of products (over the lifetime of your array) just do something like

Array.Resize<Product>(ref product, product.Length + 1);

If you are going to be adding a lot of products, you might want to do something similar to what List does - like this:

Array.Resize<Product>(ref product, product.Length * 2);
s_hewitt
  • 4,252
  • 24
  • 24
  • Ok guys thanks for all your answers....I do not want to try the List method...I will go with the Array resize...Would I use it the following way in the add button after entering details in textboxes: private void btnAdd_Click_1(object sender, EventArgs e) { Array.Resize(ref value, 5); } – Timmy Dec 03 '09 at 01:20
  • Note that since a ref parameter is required, this method cannot be used on a property of an object. – ryanwebjackson Apr 04 '18 at 18:57
3

I think you need a List<product> collection, looking at your code. Then just call the Add() method on it

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • Ok guys thanks for all your answers....I do not want to try the List method...I will go with the Array resize...Would I use it the following way in the add button after entering details in textboxes: private void btnAdd_Click_1(object sender, EventArgs e) { Array.Resize(ref value, 5); } – Timmy Dec 03 '09 at 01:21
3

You can't add items to an array, you would have to create a new array that is larger and copy the items to it. There is a method for that, which is somewhat misleadingly named Resize as it doesn't actually resize the array:

Array.Resize<product>(ref value, 5);

If you want to add items to a collection, you should use a List instead:

private List<product> value = new List<product>();
value.Add(new product("One",5));
value.Add(new product("Two",3));
value.Add(new product("Three",8));

value.Add(new product("Four",2));

Edit:
If you want to resize the array, you might want to increase the size instead of resizing it to a specific value:

int index = value.Length;
Array.Resize<product>(ref value, index + 1);
value[index] = ...
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Ok guys thanks for all your answers....I do not want to try the List method...I will go with the Array resize...Would I use it the following way in the add button after entering details in textboxes: private void btnAdd_Click_1(object sender, EventArgs e) { Array.Resize(ref value, 5); } – Timmy Dec 03 '09 at 01:20
  • Yes, but using the zero index you would rather resize it to four, or perhaps increasing the size by one (se edit above). – Guffa Dec 03 '09 at 01:50
  • Are you sure the code is correct? Am gettin errors: Error 1 The best overloaded method match for 'System.Array.Resize(ref products.value[], int)' has some invalid arguments and Error 2 Argument '1' must be passed with the 'ref' keyword – Timmy Dec 03 '09 at 02:17
  • @Timmy: Corrected. The call should of course have the ref keyword, just like in the first example. – Guffa Dec 03 '09 at 02:34
0

Arrays are zero indexed, so an array initialized to a size of 4 could only be accessed up to index 3...

If you wanted the array to grow, then you'd have to either initialize the array at least as large as you wanted to be able to grow to, or else you'd have to create a new array with the new larger size and copy the old array over; not very efficient.

In this case, you'd do better to use a collection like a list, rather than an array, so that the size can dynamically increase.

John Weldon
  • 39,849
  • 11
  • 94
  • 127
  • Ok I know that already but its not working....hmmm?? What could be the problem now if the array indexed is set to [0] to [4]? Do I need to declare it as an empty array at start as so: value[0] = new product("One",5) value[1] = new product("Two",3) value[2] = new product("Three",8) value[3] = new product{textbox1,textbox2} ? – Timmy Dec 03 '09 at 01:10
  • +1 Wow, can't believe I did not catch that too. @Timmy, your value[1] = new product("One",5) should really read value[0] = new product("One",5) – J.Hendrix Dec 03 '09 at 01:14
  • Sorry forgot to mention I do not want it to grow to big because its a test at the moment...So about 2 new products will do...will this work: private product[] value = new product[5]; value[0] = new product("One",5) value[1] = new product("Two",3) value[2] = new product("Three",8) or like this: private product[] value = new product[5]; value[0] = new product("One",5) value[1] = new product("Two",3) value[2] = new product("Three",8) value[3] = new product(textBox1,textBox2) value[4] = new product(textBox1,textBox2) – Timmy Dec 03 '09 at 01:35
0

You should probably also take a look at Array versus List: When to use which?

Community
  • 1
  • 1
Sune Rievers
  • 2,676
  • 3
  • 25
  • 29
0

Array.Resize is not an efficient method. It creates a new array with the new size then copy old array values to the new array and then replaces the old array with the new array. As you can see this is not a very efficient way of appending an item.

Unless it is a requirement that you have no control on use a List instead of array or create an array with maximum expected dimension from the beginning.

For more explanation consider this example: You created an array that can hold million items and fill it with objects. You want to add the million and one object to the array. You use Array.Resize method to change array size to 1000001. When the method tries to create a new array of 1000001 items and copy the items from old array to new array you may get an out of memory error because you are trying to have TWO arrays of a million (holding 2 millions objects) in memory at the same time while all the items that you need are one million +1. Although consider the time and resources lost in creating, copying and replacing.

Mohamed Eissa
  • 51
  • 1
  • 3