?
Solved

Dynamic grow/shrink Array in C#

Posted on 2007-03-27
2
Medium Priority
?
2,308 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 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