Solved

Arrays

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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