11

I have a .NET form, and a native code in my Visual Studio. The problem is: I can't declare a global instance of my .NET form in my native code, like this:

Editor^ maineditor;

It gives me this problem:

error C3145: 'EditorEntry' : global or static variable may not have managed type 'Cube3D::Editor ^'
bluish
  • 26,356
  • 27
  • 122
  • 180
Miguel P
  • 1,262
  • 6
  • 23
  • 48
  • 1
    The MSDN article for C3145 documents this error well. And also gives the workaround, make it a static member of ref class. – Hans Passant Jul 10 '12 at 18:23

3 Answers3

14

Instead of using a global static try making it a static method in a container type

ref class ManagedGlobals {
  public:
  static Editor^ maineditor = nullptr;
};
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • THanks, but when i define editor, the second code this comes: a variable with static storage duration cannot have a handle or tracking reference type – Miguel P Jul 10 '12 at 18:30
  • And when compiling: 'editor' : global or static variable may not have managed type 'Cube3D::Editor ^' – Miguel P Jul 10 '12 at 18:39
  • @user1492812 oops, didn't realize that was an issue. Removed that part of the answer – JaredPar Jul 10 '12 at 18:39
  • Any ideas?, Im sorry for the misconception. EditorEntry was the previous name for my instance of the Editor, sorry about that :/ – Miguel P Jul 10 '12 at 20:25
10

wrap the handle with a gcroot<> struct

gcroot<Editor^> maineditor;
user2242746
  • 369
  • 4
  • 7
0

You have your static class up top (referece: Can a class be declared static in c++?)

ref class ManagedGlobals abstract sealed {
public:
    static Excel::Application^ xl;
};

Now just reference that class

ManagedGlobals::xl = gcnew Excel::Application();
Community
  • 1
  • 1
Chad Crowe
  • 1,260
  • 1
  • 16
  • 21