Brendan Tompkins [MVP]

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted 11-10-2003 12:45 PM by Brendan Tompkins
Filed under:

[Advertisement]

Comments

James Curran wrote re: Adding System.Array Items
on 11-10-2003 8:28 AM
As a wise man once said "Closets are for Hangers/Winners use the door/So, use it Rosie/That's what it's there for..."

ArrayList was create *precisely* to fill that limitation in Array.
Scott Galloway wrote re: Adding System.Array Items
on 11-10-2003 8:00 PM
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 :-)
Brendan Tompkins wrote re: Adding System.Array Items
on 11-11-2003 1:47 AM
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?
James Curran wrote re: Adding System.Array Items
on 11-11-2003 12:00 PM
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.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?