Solved

Dynamic grow/shrink Array in C#

Posted on 2007-03-27
2
2,295 Views
Last Modified: 2012-06-21
How do I dynamically grow/shrink an array of objects I have created?

I see that I can create an array like this:

VShape_c[] m_vShapes      = new VShape_c[1000];

but I would prefer to start with ZERO in the array, and add VShape_c objects to the array as and when i want to, so that i can also query it at any time to know how many is in the collection. Currently, I have to allocate 1000 and then I maintain a member variable that tracks how many objects I REALLY have added.

There must be a better way. Can you help?





0
Comment
Question by:jhattingh
[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
2 Comments
 
LVL 13

Accepted Solution

by:
KarinLoos earned 50 total points
ID: 18799834
you could just use an arraylist.
ArrayList m_vShapes      = new ArrayList();
To add an item. just use the add method.
m_vShapes.Add( someitem );

0
 
LVL 18

Expert Comment

by:DarrenD
ID: 18799965
Hi,

An ArrayList is a good way to go but it operates on objects only and so you will get boxing / unboxing which may cause performance problems.

If you are using Visual Studio 2005 there is a method called Resize in .NET 2.0 which does the same as ReDim in VB.NET

This is taken from the Visual Studio 2005 Documentation.

String[] myArr = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};

// Resize the array to a bigger size (five elements larger).
Array.Resize(ref myArr, myArr.Length + 5);

This would enable you to start with one element and add more as needed.

Another alternative would be to use a collections.

Hope this helps,

Darren
0

Featured Post

Industry Leaders: 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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.

739 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