Reputation: 125
I've been learning C++/CLI for a few months now, but no matter what I try, I can't seem to fix a problem I'm having.
I need to pass either a String^, Array^, or an ArrayList^ data type from main.cpp to Form1.h. I've attempted to make variables global in main.cpp and then call the variable using extern. However, this will not work for String, Array, and ArrayList data types.
How would I go about doing this? Thanks in advance. Here is a paraphrase of my code:
//main.cpp
bool LoadFileFromArgument = false; //A boolean can be declared global
String^ argument; //this will not pass from main.cpp to Form1.h
int main(array<System::String ^> ^args)
{
String ^ argument = args[1]
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
//Form1.h
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
extern bool LoadFileFromArgument; //is grabbed without error
extern String^ argument; //will not work
Here is the error:
error C3145: 'argument' : global or static variable may not
have managed type 'System::String ^'
may not declare a global or static variable,
or a member of a native type that refers to objects in the gc heap
Upvotes: 2
Views: 3021
Reputation: 6854
Can you not create an overloaded constructor for the form. i.e.
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(String^ argument)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
// Use "argument" parameter as req'd.
}
Form1(void)
{
//....usual constructor here...
//..etc...
then from main
// Create the main window and run it
Application::Run(gcnew Form1(argument));
Upvotes: 2