Solved

Dynamic grow/shrink Array in C#

Posted on 2007-03-27
2
2,285 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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