I am writing an application (well, re-writing it as I'm porting the code from an antique environment (Borland C++5) to attempt to overcome several instabilities in modern versions of Windows) which uses two classes - one stores and processes data from an instrument, or saved data from file, and the other holds assorted parameters for the instrument and data analysis. I have defined a Windows Form that displays the outputs from the instrument and associated processed data, with worker threads to handle the non-GUI elements. All going OK so far, with one big exception at present.
The problem I'm having is with accessing my class data from multiple functions within the form. Here's what I'm trying to do (excert from code below). I want to set the configuration class as global within the Form, I have declared it and initialized values within the constructor. The code snippet includes a couple of boolean flags that I likewise declare and initialize (there are other int, float, String^ variables I'm also using). When the code runs, it launches the Form and the code in OnShown() is run, which contains the guts of the program, calling the worker threads as needed, calling other functions to update the GUI etc.
public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); AEROConfig ^Config = gcnew AEROConfig(); AcquireNewData = false; ReplayOldData = false; } protected: ~Form1() { if (components) { delete components; } } public: AEROConfig ^Config; // Configuration data public: bool AcquireNewData; // set to true for acquisition of new data bool ReplayOldData; // set to true for replay spectral data private: System::Void OnShown(System::Object^ sender, System::EventArgs^ e); }
What I've got in OnShown() starts with this code (again, some other stuff that works is removed - including a function to select the FileName). LoadConfigFile() is a function within the AEROConfig class that reads the configuration data from a text file (in this case test.con).
System::Void Form1::OnShown(System::Object^ sender, System::EventArgs^ e) { String^ FileName = "C:test.con"; // AEROConfig ^Config = gcnew AEROConfig(); Config->LoadConfigFile(FileName); }
As I currently have the code, within OnShown Config is undefined, and the program crashes trying to call a function it doesn't know (LoadConfigFile()). The other parameters I define in Form1.h (AcquireNewData, ReplayOldData etc) are available in OnShown(). If I put the initialisation of Config into OnShown() (where it's commented out above) and remove it from the constructor then it works. But, I then need to explicitely pass Config to other functions defined in Form1.h (which I haven't shown for brevity) which is cumbersome and doesn't seem to be the way I should be doing it. In addition, as I've got further in translating the old code I've tried to put in another form that allows the user to amend the configuration data. I'd like this to be called from Form1 when an appropriate button is clicked. This call will be independent of OnShown(), and so the initialised Config from OnShown() is not accessible to it - though if Config was global within Form1 I would have access to it.
So, my question is ... what am I doing wrong? Why when I define Config and initialise it in the constructor isn't it defined in other functions in the Form? Especially when doing essentially the same thing for other variable types does what I expect. BTW, I know the code has things listed as public: I have had them as private: as well, but that doesn't seem to make an big difference.
Alan