none
may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap RRS feed

  • Question

  • Hey guys,

    I know this topic has come up many many times, but I haven't found any sources that help apply it to my situation. I'd like to declare the following line as a global variable, but compiler gives error: may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap.

    cli::array<double> ^ Buffer1 = gcnew cli::array<double>( 10 );

    thanks big time!

    Naisan
    Tuesday, September 23, 2008 6:51 PM

Answers

  • You need to declare this as a static field of a managed class.  Managed code does not have anything that directly corresponds to global variables; static fields achieve the same purpose (but with more "organization").

    For example:

    public ref class Globals  
    {  
    public:  
      static cli::array<double> ^ Buffer1 = gcnew cli::array<double>( 10 );  
    }; 

    An example use would be as follows:

    double x = Globals::Buffer1[0]; 
    • Marked as answer by nobugzModerator Wednesday, September 24, 2008 9:57 AM
    • Unmarked as answer by Naisan_Yazdani Wednesday, September 24, 2008 2:36 PM
    • Marked as answer by Naisan_Yazdani Wednesday, September 24, 2008 2:40 PM
    Wednesday, September 24, 2008 1:19 AM