12

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 ^' 
1
  • 1
    The MSDN article for C3145 documents this error well. And also gives the workaround, make it a static member of ref class. Commented Jul 10, 2012 at 18:23

3 Answers 3

15

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

ref class ManagedGlobals { public: static Editor^ maineditor = nullptr; }; 
Sign up to request clarification or add additional context in comments.

4 Comments

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
And when compiling: 'editor' : global or static variable may not have managed type 'Cube3D::Editor ^'
@user1492812 oops, didn't realize that was an issue. Removed that part of the answer
Any ideas?, Im sorry for the misconception. EditorEntry was the previous name for my instance of the Editor, sorry about that :/
10

wrap the handle with a gcroot<> struct

gcroot<Editor^> maineditor; 

1 Comment

Works like a charm and cleaner than creating a dummy wrapper class :-)
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(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.