what is the syntax of a __gc array?

I declare an array in .h file as the following:
 public __gc class myClass
 {
 public:
  int myIntArray __gc[];
。。。

in the class constructor:

myIntArray= __gc new int[5];
for (int i=0;i<2;i++) myIntArray[i]=i;

what I get are compiler errors, as follows:

Lib2.cpp(22) : error C2726: “__gc new”can not create __gc object
Lib2.cpp(22) : error C2440: “=” ....

What is the proper syntax?
Thanks!  
CFreeAsked:
Who is Participating?
 
drichardsConnect With a Mentor Commented:
Correct syntax is:

    public __gc class myClass
   {
      public:
      int myIntArray __gc[];
    };


then:

    myIntArray= new int __gc[5];
    for (int i=0;i<2;i++) myIntArray[i]=i;
0
 
Jaime OlivaresSoftware ArchitectCommented:
Hi CFree,

Just use

public __gc class myClass
 {
 public:
  int myIntArray *__gc;
   // etcetera
}

don't forget to delete your array in the destructor

Good luck,
Jaime.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Not solved your problem?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
CFreeAuthor Commented:
hi Jaime,
since the array declared as the managed code, why need i delete it in the destructor?
Thanks!
0
 
Jaime OlivaresSoftware ArchitectCommented:
In that case, please forget my last line, but my first suggestion is correct.
0
 
CFreeAuthor Commented:
replaced wtih  int myIntArray *__gc;
the complier gives me the following errors:
e:\CurrentProj\GE\Lib2\Lib2\Lib2.h(26) : error C2143:syntax error....
e:\CurrentProj\GE\Lib2\Lib2\Lib2.h(26) : error C3150:
e:\CurrentProj\GE\Lib2\Lib2\Lib2.h(26) : error C2059:
e:\CurrentProj\GE\Lib2\Lib2\Lib2.h(26) : error C2238:
0
 
Jaime OlivaresSoftware ArchitectCommented:
Is your myIntArray class being reconized at that point?
Maybe you have not #included proper header.
0
 
CFreeAuthor Commented:
public:
  int myIntArray *__gc;

I replaced  " int myIntArray __gc[] " with the above section  in the head file.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.