Solved

Dynamic grow/shrink Array in C#

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

724 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