Using integers in a C++ ArrayList

In this day and age, creating a dynamic list of integers seems like it should be an easy task. I set out to figure out how to do this using the ArrayList class in DotNet, I actually found it to be more of a challenge than it should have been.

As evidenced by the lacking DotNet C++ examples in MSDN, it seems Microsoft has been drifting away from C++ in leiu of C#. As a result, it took me quite a while to figure out the complicated casting and boxing combination required to extract a simple Int16 type from the ArrayList. To an advanced DotNet deveoper, perhaps this is obvious, but to a novice with poor examples to work with, this simple task was very frustrating.

Int16 Number;
ArrayList *NumberList;
System::Collections::IEnumerator* myEnumerator;
int i = 0;

NumberList= new ArrayList();
Number = 1;
NumberList->Add(__box(Number));

myEnumerator =  NumberList->GetEnumerator();
while (myEnumerator->MoveNext())
{
     Number = *dynamic_cast(myEnumerator->Current);
    // Do something with Number
}


The other alternative is to derive a child class from ArrayList that returns a custom Enumerator that has the desired type for the “Current” member. I really wonder why Microsoft did not provide this as part of the standard library.

Please leave a comment if you have found a better way to do this.

Advertisements
This entry was posted in C++, DotNet, Programming. Bookmark the permalink.

7 Responses to Using integers in a C++ ArrayList

  1. Jason,

    I don’t know if I can post code in this comment so I won’t even try lest WordPress mangle things…

    I’ll tell you up front that I’ve never used .NET.

    Nonetheless, I tried to look at the class and it seems that you still have to do the box/cast combo though, since the ArrayList just holds Objects, ie it is not a strongly-typed container. This is analagous to the non-templated versions of Java collections (tho Java 1.5 now supports specifying the type that the collection contains, afaik). My flippant answer is: Use std::vector and the square-brackets to access the elements 😛

    But in talking with Rob, I know you guys are fully in bed with Microsoft at this point 😛

    Anyway, it seems that if you want to avoid using an iterator (i.e. if you just want to get at one value), you could save a little grief by just using the Item property, i.e. get_Item(index).

    Is .NET’s Array better for your application? It seems that you can specify the Type of the Array and that might bypass a lot of the casting crap…

    Regards,
    Jeff

  2. I thought I’d try to post my Standard C++ version of your example for shiggles. I’m assuming Int16 is either a typedef of “short”, or a fully copyable C++ class.

    Int16 Number;
    std::vector Numbers;
    std::vector::iterator it;

    Number = 1;
    Numbers.push_back(Number);

    it = Numbers.begin();
    while (it != Numbers.end() )
    {
    Number = *it;
    ++it;
    // Do something with Number
    }

  3. Nope, WordPress ate my code. std::vector should have a < Int16 > after it

  4. Jason says:

    Yes, I can use the get_item(index) method instead of the iterator, but it still requires the boxing and casting.

    You’re also right about using the Array type to avoid the casting, but it is not dynamic. The sized is fixed at compile time.

    I’ll have to try out your vector method, that may be just what I’m looking for. Thanks!

  5. Sam Vander says:

    Hey thanks for the help I’m going to try out the vector method looks easier.

  6. outblu says:

    I’ll try to figure out something in java 😉

  7. Nathaniel Zhu says:

    Thanks, very useful code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s