0

I am currently creating a program where the user can use a printer as long as that particular user has enough funds.

The current issue I am having is that if the user chooses to have colour printing instead of black and white then the price for each piece of paper goes up.

How do I add value to an already existing array?

Here is my code...

printers[0] = new Printer("printer1", 0.10M);
            printers[1] = new Printer("printer2", 0.08M);
            printers[2] = new Printer("printer3", 0.05M);
            printers[3] = new Printer("printer4", 0.15);
            printers[4] = new Printer("printer5", 0.09M);

            foreach (Printer r in mPrinters)
            {
                if (printer != null)
                    printerCombo.Items.Add(r.getName());
            }
2
  • 5
    use a List<T> instead Commented Jan 20, 2017 at 19:54
  • Arrays are of fixed size by design. If want a collection that you can add, remove, insert then look at the classes in System.Collection.Generic or System.Collection.ObjectModel Commented Jan 20, 2017 at 20:00

3 Answers 3

3

Technically, you can Resize the array:

 Array.Resize(ref printers, printers.Length + 1);

 printers[printers.Length - 1] = new Printer("printer6", 0.25M);

However, a much better approach is to change the collection type: array into List<T>:

 List<Printer> printers = new List<Printer>() {
   new Printer("printer1", 0.10M),
   new Printer("printer2", 0.08M),
   new Printer("printer3", 0.05M),
   new Printer("printer4", 0.15),
   new Printer("printer5", 0.09M), }; 

 ...

 printers.Add(new Printer("printer6", 0.25M));
Sign up to request clarification or add additional context in comments.

2 Comments

Resize creates new a copies all the contents of the old array into the new one. It doesn't resize the original array.
@Fran: you are quite right (that's why we pass the array with ref when we call Array.Resize). However, this underhood behaviour, IMHO, doesn't matter much: we get the array with its Length increased and we know that the arrays are not supposed to change their sizes (so we should not use Array.Resize frequently)
1

Arrays have fixed size - after you created array with size 10 you can't add one more element (to make size 11).

Use List<Printer>:

List<Printer> printers = new List<Printer>();
printers.Add(new Printer("printer2", 0.08M));
//add all items

Also you can access elements by index:

var element = printers[0];

Using List you can change its size, add and remove elements.

Comments

0

Arrays are fixed length. You need to copy the values into a new array or use an List, List<>, or ArraryList.

4 Comments

No, they are not immutable. They just have a fixed length. Two different things.
true. updating answer.
ArrayList is pretty much outdated and we shouldn't advise using it. List<T> is the way to go.
Yes. I prefer stingy typed collections, but it's still part of the framework

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.