Click to See Complete Forum and Search --> : Arrays of User-Defined Classes


functor
April 27th, 2005, 04:48 PM
Heya!

I'm converting some unmanaged code into managed code. I've got several classes that I have created. The problem I'm running into occurs when I try and create a data member in one of my classes that is an array of another class I created. Everything worked fine when it was unmanaged, but now I'm getting an error on the array declaration: error C2691: 'hall' : invalid type for __gc array element

Here are 2 of my classes .h files:

game.h
-----------------
#pragma once
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

__gc class game
{
public:
game(void);
~game(void);

static char* gameNumber = new char[MAX_STRING_LENGTH];
static char* currentBooked = new char[MAX_STRING_LENGTH];
static char* lastReBooked = new char[MAX_STRING_LENGTH];
static char* printed = new char[MAX_STRING_LENGTH];
static char* transmitSales = new char[MAX_STRING_LENGTH];
static char* transmitSalesSuccess = new char[MAX_STRING_LENGTH];
static char* gameStatus = new char[MAX_STRING_LENGTH];
};



hall.h
-----------------
#pragma once
#include "game.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


__gc class hall
{
public:
hall(void);
~hall(void);
void getGames(void);
void getPrinted(char *, char *);
void getPreviousBooked(char *, char *);
void getTXSuccess(char *, char *);
void stringToCharArray(char *, String *);

static char* hallCode = new char[MAX_STRING_LENGTH];
static char* hallName = new char[MAX_STRING_LENGTH];
static char* stationID = new char[MAX_STRING_LENGTH];
static char* stationName = new char[MAX_STRING_LENGTH];
static char* hallStatus = new char[MAX_STRING_LENGTH];

game hallGames[MAX_GAMES_PER_HALL]; // THIS IS WHERE I GET THE ERROR!
int numGames;
};



I tried doing: game *hallGames = new game[MAX_GAMES_PER_HALL];
I also tried it with a static thrown infront. Nothing seems to work.

How do i properly declare an array of another class as a data member in a managed class?

dumbquestion
April 27th, 2005, 05:15 PM
In the past, I've created an array of pointers to a class I defined like this:

MyClass *myclassarray[] = new MyClass* [5];


I then did a loop to initialize all 5 MyClass objects:

for (i=0;i<5;i++)
{
myclassarray[i] = new MyClass();
}

So if there was some method "somemethod" in the MyClass class, you could call it like this for the third MyClass in myclassarray:

myclassarray[2]->somemethod(....);



There's always the ArrayList way of doing it:

ArrayList *myclassarray = new ArrayList();
MyClass *_myclass;

for (i=0;i<5;i++)
{
_myclass = new MyClass();
myclassarray->Add(_myclass);
}

But then you would have to typecast if you were trying to reference one of the elements:

MyClass *newmyclass;
newmyclass = dynamic_cast<MyClass*>(myclassarray->get_Item(2));

newmyclass->somemethod(....);


Hope it helps...

Andy Tacker
April 28th, 2005, 06:30 AM
take a look at the following namespace:
System.Collections.CollectionBase
there are examples in MSDN to refer to...