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