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<__box Int16*>(myEnumerator->Current);
// Do something with Number
}
Please leave a comment if you have found a better way to do this.
March 29th, 2005 at 2:30 pm
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
March 29th, 2005 at 2:35 pm
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
}
March 29th, 2005 at 2:36 pm
Nope, WordPress ate my code. std::vector should have a < Int16 > after it
March 30th, 2005 at 1:09 pm
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!