[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

C# resize and add items to an array

Posted on 2013-07-01
8
Medium Priority
?
471 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 45

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 64

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 45

Accepted Solution

by:
AndyAinscow earned 1500 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 45

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

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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