Solved

Arrays

Posted on 2006-11-29
3
332 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
ID: 18038338
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
ID: 18038407
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
ID: 18038435
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 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