views:

745

answers:

6

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?

+1  A: 

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
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
+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
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
+13  A: 

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

EDIT:

Please stop UPVOTING ME for such an trivial answer!!! I don't want all this rep or votes from a simple answer, thanks ;) (community wiki now)

Pop Catalin
+1 Then call list.ToArray() if you want the array
SwDevMan81
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<product>(ref value, 5); }And how would I use the ArrayList?
Timmy
Timmy, you should use whatever makes more sense for you, but just so you know, `List<T>` and `ArrayList` both use internally a private array that is resized using Array.Resize() automatically when adding or removing items.
Pop Catalin
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
+3  A: 

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

Russ Cam
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<product>(ref value, 5); }
Timmy
+5  A: 

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
+1 Big fan of List<T> but this is nice too.
J.Hendrix
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<product>(ref value, 5); }
Timmy
How many products do you plan on adding? Check my edit.
s_hewitt
+2  A: 

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
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<product>(ref value, 5); }
Timmy
Yes, but using the zero index you would rather resize it to four, or perhaps increasing the size by one (se edit above).
Guffa
Are you sure the code is correct? Am gettin errors:Error 1 The best overloaded method match for 'System.Array.Resize<products>(ref products.value[], int)' has some invalid arguments andError 2 Argument '1' must be passed with the 'ref' keyword
Timmy
@Timmy: Corrected. The call should of course have the ref keyword, just like in the first example.
Guffa
A: 

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

Sune Rievers