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 );
}
}

Passing a Form handle to a class constructor
Bob_dlonra
Oh. Your code in MyForm.h is refering to a namespace which probably hasn't been defined yet. I believe you can do a forward declaration of the namespace.
oystagoymp
Nothing wrong with doing it that way that I'm aware of. However, as a matter of style and in the spirit of information hiding, I'd keep txtTestValue private, and access its value through a public read-only property.
kumarvk
Sorry, this is a cleaned-up example. The closing curly bracket for the namespace is missing and the ending semi-colon is missing.
If I remove the MyClass constructor, the program compiles correctly.
trogdor2323
Adding this to MyClass.h has worked. Thanks!
namespace
MyProject { ref class Form1;};
steveareno
sarika
Tryston02
Ashish Garg
Your class definition for Form1 doesn't seem to be closed properly -- the closing braces don't match up. Class definitions need to be followed with a semi-colon. Is the code you posted the code you're actually using, or have you cleaned it up before posting it
Saeideh
I added the namespace definition but I get compiler errors:
error C2039: 'Form1' : is not a member of 'MyProject'
error C2061: syntax error : identifier 'Form1'
Yustme
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.
eweaver
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.
Pat Murphy
Sorry, I should have explained that more thoroughly. Add the following code to your MyForm.h file, after the #pragma directive:
namespace MyProject {}
Does this fix it