Solved

Arrays

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS2010 Build fails to install 14 102
Unlocking a column in excel using C# 17 33
C# DataTable (in memory) vs SQL Lookups 4 75
Need a quick overview of Selenium testing 2 36
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…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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