__gc and Array

L

Lupina

#pragma once

using namespace System;

__gc class CWords

{

public:

CWords(void);

~CWords(void);


int iPassedItems[1000];

};



How to add array of integers to my class?

int iPassedItems[1000]; - makes error :

error C2697: 'iPassedItems' : must explicitly specify __gc or __nogc for an
array declared in a managed type

I know, that I have to use __gc in my array, but I don't know how to use it.

Please Help
 
W

William DePalo [MVP VC++]

Lupina said:
How to add array of integers to my class?

int iPassedItems[1000]; - makes error :

error C2697: 'iPassedItems' : must explicitly specify __gc or __nogc for an
array declared in a managed type

I know, that I have to use __gc in my array, but I don't know how to use it.

This declares an array whose elements are of the managed Object class:

Object *parms __gc [];

This allocates an array of n elements:
parms = new Object * __gc [n];

Regards,
Will
 
G

Guest

Hi Lupina,
You could use something like

#pragma once

using namespace System;

__gc class CWords
{

public:

CWords(void){}

~CWords(void){}

static int iPassedItems __gc[] = new int __gc[1000];
};

In case you want to initialize a member inside a class it must be static.
Thanks,
Kapil
 
L

Lupina

Hi Will

I did it acording your hint

__gc class CWords

{

public:

CWords(void);

~CWords(void);

int iPassedItems __gc[];

};

CWords::CWords(void)
{

iPassedItems = new int * __gc [100];

}



but, I got error :

error C2691: 'int __gc *' : invalid type for __gc array element

:(
 
W

William DePalo [MVP VC++]

Lupina said:
I did it acording your hint

__gc class CWords

{

public:

CWords(void);

~CWords(void);

int iPassedItems __gc[];

};

CWords::CWords(void)
{

iPassedItems = new int * __gc [100];

}



but, I got error :

error C2691: 'int __gc *' : invalid type for __gc array element

:(

I showed you how to create an array of managed, heap-based,
garbage-collected objects. An integer is a "value" (loosely: stack based)
type. You don't need special syntax.

Change

int iPassedItems __gc[];

to

int *iPassedItems;

Change

iPassedItems = new int * __gc [100];

to

iPassedItems = new int[100];

Regards,
Will
 
L

Lupina

It's working at last. I wasn't able to advise myself with it.
Both versions are operating now (Will, Kapil).

Great Thanks
 
L

Lupina

I still have the question, conected indirectly with previous.

I've created a class in which I want to keep global date (available in a few
forms).

The object of this class I have in the main form :


#pragma once

#include "Words.h"
#include "DialogOptions.h"

namespace Aplication
{
....

public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
myCWords = new CWords();
dlgOptions = new DialogOptions();

}
....
public : CWords *myCWords;
public : DialogOptions *dlgOptions;
....



now in the other form (eg: DialogOptions) in the top of DialogOptions.h,
when I add

#include "Form1.h"

I recieve an error :
Form1.h(47): warning C4677: 'myCWords': signature of non-private function
contains assembly private type 'CWords'



How to refer to the first form (from second)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top