Passing a Form handle to a class constructor
I'm just starting to use Managed C++ and .NET. I'm still in the early learning stages.
Basically, I have a Window Form that has a value for MyClass to use. I thought I could use the following code to pass the Form1 handle to the Myclass constructor. First, the compiler says MyProgram is not a namespace. I've placed the using namespace into the MyClass.h, but this dosesn't help
I think it may be a chicked before the egg kind of thing, but want to make sure I'm not missing something.
Currently, I have a private member method within the Form1 class called setMyClass( ^Form1 ) which works but this doesn't seem very OOP to me.
//MyClass.h
#pragma once
public ref class CMyClass {
public:
CMyClass( MyProgram::Form1 ^hForm );
private:
int myTestVar;
}
//MyClass.cpp
#pragma once
#include "StdAfx.h"
#include "MyClass.h"
CMyClass::CMyClass( hForm ){
myTestVar = Convert::ToInt32( hForm->txtFieldFromForm1->Text );
}
//Form1.h
#pragma once
#include "MyClass.h"
namespace MyProgram {
public ref class Form1 : public System::Windows::Forms::Form {
public:
Form1(void) {
// initializing stuff
}
protected:
//destructor
private:
private: System::Void btnInitMyClass_Click(System::Object^ sender, System::EventArgs^ e) {
CMyClass MyClass = gcnew CMyClass( this );
}
}
OK, I should say it compiled OK. I'm still having some trouble and I'm sure this will be a dumb one.
CMyClass::CMyClass( MyProject::Form1 ^hForm ) {
MyProject::Form1 ^hTest; //this is just for testing.
int myTestValue;
hTest = hForm;
// myTestValue = Convert::ToInt16( hTest->txtFromForm->Text );
}
With the myTestValue commented out, the program compiles fine. When I run the debugger, I come to this constructor and hForm and hTest have valid entries.
When I remove the comment in front of myTestValue, the compiler gives me:
1>.\MyClass.cpp(12) : error C2027: use of undefined type MyProject::Form1'
1> c:\documents and settings\john thomasser\my documents\visual studio 2005\projects\praxflow\praxflow\MyClass.h(4) : see declaration of 'MyProject::Form1'
1>.\MyProject.cpp(12) : error C2227: left of '->txtFromForm' must point to class/struct/union/generic type
1>.\MyProject.cpp(12) : error C2227: left of '->Text' must point to class/struct/union/generic type
1>Build log was saved at "file://c:\Documents and Settings\John Thomasser\My Documents\Visual Studio 2005\Projects\Praxflow\Praxflow\Debug\BuildLog.htm"
1>Praxflow - 3 error(s), 0 warning(s)
I've tried various things with no luck.
Well, I found a solution. I'm not sure I like it of if it is the optimal.
First, I needed to change the private declaration of Form1:
public: System::Windows::Forms::TextBox^ txtTestValue;
Then I added #inclue<Form1.h> to MyClass.cpp
Is this the only answer? Seems like a kludge.