Solved

Dynamic grow/shrink Array in C#

Posted on 2007-03-27
2
2,281 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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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…

771 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

14 Experts available now in Live!

Get 1:1 Help Now