Solved

Arrays

Posted on 2006-11-29
3
326 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

930 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

10 Experts available now in Live!

Get 1:1 Help Now