Solved

C# resize and add items to an array

Posted on 2013-07-01
8
462 Views
Last Modified: 2013-07-02
Is is possible to resize an array in C#? I want to loop through an array and add items as the user adds them. So the array size will need to change each time a new item is added. I do not have any experience working with arrays. Thanks.
0
Comment
Question by:dodgerfan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39290250
Why not use a List - just add (or insert) items as required.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39290252
You have to create a new array of the new size, then copy the items over. The Array class has a Copy method that you can use.
0
 

Author Comment

by:dodgerfan
ID: 39290366
I initialize and fill the array when it starts. The program asks if the user would like to add a name. If yes, then I want to let them add a name, which means resizing the array. Array.Resize is what I was thinking. A snippet of what I have so far is below:

            Console.WriteLine("\nWould you like to add a name?");
            addName = Console.ReadLine();
            if (addName == "y")
            {
               // here is where i need to loop and resize, but I'm not sure.
                Array.Resize<string>(ref customerNames, 7);
                Console.WriteLine("Enter the name");
                customerNames[6] = Console.ReadLine();
            }
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39290368
Hi dodgerfan;

I would use a List<> object because you can add and remove the objects in the List very simply and if you need to send it to a function that is expecting a Array you can use the ToArray() method on the list and it will return an array already created for you.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 39290488
Is there a reason you don't want a list as I suggested earlier.  Just use .Add(new item goes here) - no need to do anything else yourself.
0
 

Author Comment

by:dodgerfan
ID: 39291609
I know about the list as mentioned. I'm working through arrays and trying to get a better grip on them. I have the users picking from a list of choices. Add is one. When they want to add a name, it works, but when they enter another one the array is not displaying all of the names, even though it has increased in size. I put the switch inside a do loop.

           myChoice = Console.ReadLine();
            
           // decide what to do based on the users selection
           switch(myChoice)
           {
            case "A":
            case "a":
                  Console.WriteLine("You want to add a name.");

                  IncreaseArray(ref customerNames, 1);
                  Console.WriteLine("Enter a name to add:");
                  customerNames[6] = Console.ReadLine();

                  Console.WriteLine("The array is now holding {0} names\n", customerNames.Length);
                  Console.ReadLine();
                  Console.WriteLine("The names in the array listed alphabetically are as follows:\n");
                              for (int x = 0; x < customerNames.Length; ++x)
                              {
                                    Console.WriteLine("{0}\n", customerNames[x]);          
                              }
                  Console.ReadLine();
                  break;

the increase arrray:
       public static void IncreaseArray(ref string[] values, int increment)
       {
       string[] array = new string[values.Length + increment];
      values.CoptTo(array, 0);
      values = array;
        }
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39294617
If you're destined to use an array, may I ask why you selected the answer that you did?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39295570
I agree with kaufmed - that doesn't fit with your comment adressing my question.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question