CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

Adding System.Array Items

OBSOLETE CONTENT
The author of this post has determined that this content is obsolete. Use at your own risk! Blog posts are a point-in-time snapshot of the blogger's thinking and should not be assumed to represent this blogger's current opinions. This post was left up for historical purposes.

So, I see this type of pattern in a bunch of different places in my code where I use arrays of objects contained in other objects.  Often, I want to use a System.Array object, like string [] instead of an ArrayList or other collection for the simplicity, ease of serialization, etc. But, I find myself re-writing code that looks like this do perform the simple task of adding an item on the end of the array:

[Serializable]
public class
WatchListStatus
{
    private WatchListStatusItem [] m_statusItems;

    public void AddStatusItem(WatchListStatusItem item)
    {
       
// Get the length
       
int intLength = (this.m_statusItems == null) ? 0 : this
.m_statusItems.Length;
        WatchListStatusItem [] tmpStatus = WatchListStatusItem[intLength + 1];

       
// Copy items if needed
       
if(intLength > 0) Array.Copy(this
.m_statusItems, tmpStatus, intLength);

       
// Add to the end of the array
       
tmpStatus[intLength] = item;

       
// set the private instance 
       
this
.m_statusItems = tmpStatus;
    }
}

So, here's my question for everyone... Is there a better way?  I know I could do something generalized with reflection, but I'm not sure that the payoff would be that great.  I guess I'm searching for a static method of the System.Array class that could append an object, but I'm not aware of one.

Music tip - Wanna know who's coming to town? Check out Pollstar



Comments

Scott Galloway said:

Yup...nice thing is you can just use the ToArray method of the arraylist to give you a 'real' array when you need it - I typically implement a couple of methods in an inherited class called ToStringArray and ToIntArray - guess what they're for :-)
# November 10, 2003 8:00 PM

Brendan Tompkins said:

You may be convincing me to just go with an ArrayList, but a simple array does have advantages. No casting, strongly typed, lean, more portable. And what's the big difference between implementing ToStringArray and adding an Append method? You're still adding code so I'm not sure I see the big payoff?
# November 11, 2003 1:47 AM

James Curran said:

Speed mostly. ArrayList is probably implemented as a linked-list to allow for fast insertions. Array is probably implemented as contiguous block, for fast access. Choose the task you want done fast.
# November 11, 2003 12:00 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News

MVP
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.