Solved

Arrays

Posted on 2006-11-29
3
318 Views
Last Modified: 2010-04-16
I have a couple of questions with arrays:

1) if I setup and array like this so:
         public static string[] nameArray = { "Greg" };
    Is that array able to grow beyond that one? In other words can I add additional names to the array?  or do I have to define a size?  

Guess I am just confused and trying to understand them.

2) I am creating a function to reset the apps variable to a default set of values.  In it I have 2 arrays.  How do I reset them to the above default value? I.e. nameArray only having Greg in it and no others

Thanks for the help.

Will split points if need be.
0
Comment
Question by:sneeri_c
3 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 75 total points
Comment Utility
To make an array grow/shrink you create a new array and copy the values over.

If you want a truly dynamic structure then use an ArrayList instead.
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 150 total points
Comment Utility
Hi,

1) The way you have declared the array also implicitly sets the size of the array to 1 with the string "Greg" in index position 0. You can't then add another item in index 1 for example - although you can overwrite "Greg" in index 0 with a new string, e.g:  

nameArray[0] = "New String";

You can also reinitialize the array if needed (although not often done, use dynamic collections if you need to add an unknown amount of items to an array -- see ArrayList below):

nameArray = new string[2]; // reinitializes nameArray to size 2, each position is set to null

The re-initialization loses all the existing values in the old array, if you want to retain those then you need to create another array and move the values from the old array across.

If you are after a dynamic collection then you can use the ArrayList, in such a collection the upperbound is limitless, you can continue adding/removing items as you wish:

ArrayList al = new ArrayList();
al.Add("Greg");
al.Add("Smith");

You can also access the positions using an indexer:

al[0] = "John"; //overwrites "Greg" with "John"

2)

If its the same array with size 0 then just overwrite position 0 with "Greg":

nameArray[0] = "Greg";

If its a different array with a different size, reinitialise the array with "Greg" as the only item in the array:

nameArray = new string[] {"Greg"};
0
 
LVL 18

Assisted Solution

by:DarrenD
DarrenD earned 25 total points
Comment Utility
In .NET 1.1 you have to do as said above.

In .NET 2.0 you use the Resize property of the array which does the above for you.

Darren

Here is an article on resizing

http://www.informit.com/guides/content.asp?g=dotnet&seqNum=129&rl=1

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# remove trailing commas from csv file 10 44
Trouble with References... 5 21
Long URL in SMS 6 21
COnsume rest client 6 4
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now