Managed version of std::vector?

I would like to use a std::vector in my managed class. The vector will hold a number of instances of another class I made, this class is managed too. How to do this?
LVL 3
bjqrnAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DeanHorakCommented:
For managed C++ you should probably use ArrayList

#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void PrintValues( IEnumerable* myList );
 
void main()  {
 
    // Creates and initializes a new ArrayList.
    ArrayList* myAL = new ArrayList();
    myAL->Add(S"Hello");
    myAL->Add(S"World");
    myAL->Add(S"!");
 
    // Displays the properties and values of the ArrayList.
    Console::WriteLine( "myAL" );
    Console::WriteLine( "\tCount:    {0}", __box(myAL->Count) );
    Console::WriteLine( "\tCapacity: {0}", __box(myAL->Capacity) );
    Console::Write( "\tValues:" );
    PrintValues( myAL );
}
 
void PrintValues( IEnumerable* myList ) {
    System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
    while ( myEnumerator->MoveNext() )
       Console::Write( "\t{0}", myEnumerator->Current );
    Console::WriteLine();
}

 /*
 This code produces the following output.
 
 myAL
     Count:    3
     Capacity: 16
     Values:    Hello    World    !
 */

bjqrnAuthor Commented:
I've tried with the ArrayList, but how do I call something in my class?

-----------------------------------------------------------------------------------------------
  System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
    while ( myEnumerator->MoveNext() ) {

        myEnumerator->Current->myFunction();
    }
-----------------------------------------------------------------------------------------------

Doesn't seem to work.
AlexFMCommented:
Take a look at System.Collections.Generic Namespace.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

bjqrnAuthor Commented:
But I use vs.net 2003, otherwise that seemed to be it.
DeanHorakCommented:
Try changing

 while ( myEnumerator->MoveNext() ) {

        myEnumerator->Current->myFunction();
    }

to

 while ( myEnumerator->MoveNext() ) {

        MyClass *mc = (MyClass *)myEnumerator->Current;
        mc->myFunction();
    }
bjqrnAuthor Commented:
MyClass *mc = (MyClass *)myEnumerator->Current;

produces:

error C2440: '=' : cannot convert from 'System::Object __gc *' to 'MyClass __gc *'
DeanHorakCommented:
Or if you prefer

while ( myEnumerator->MoveNext() ) {
       ((MyClass *)myEnumerator->Current)->myFunction();
    }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DeanHorakCommented:
Sorry, I should have clarified..

MyClass, in the example I show refers to the name of the class that is being strored in the ArrayList.  Whatever the name of the class is that you are storing in the ArrayList should be substituted for MyClass...

Also... your class should be a managed class as well. To specify that it is a managed class include  __gc on the class definition...

it.

__gc class MyClass {
.
.
.

}
bjqrnAuthor Commented:
Yes, that's exacyly as it's done.

I rewrote the error message to make it more readable for anyone else than me.
DeanHorakCommented:
>>error C2440: '=' : cannot convert from 'System::Object __gc *' to 'MyClass __gc *'
Hmmm...

I can't see why it won't allow "MyClass __gc *" to be converted to "System::Object __gc *".  Are you sure that your class is a managed class? I've only see this error when trying to convert a nonmanaged class to the manged class, in which case the error would have been:

error C2440: '=' : cannot convert from 'System::Object __gc *' to 'MyClass  *'

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.