• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 837
  • Last Modified:

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?
0
bjqrn
Asked:
bjqrn
  • 5
  • 4
  • 2
1 Solution
 
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    !
 */

0
 
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.
0
 
AlexFMCommented:
Take a look at System.Collections.Generic Namespace.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

 while ( myEnumerator->MoveNext() ) {

        myEnumerator->Current->myFunction();
    }

to

 while ( myEnumerator->MoveNext() ) {

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

produces:

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

while ( myEnumerator->MoveNext() ) {
       ((MyClass *)myEnumerator->Current)->myFunction();
    }
0
 
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 {
.
.
.

}
0
 
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.
0
 
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  *'

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now