asimov500
Programmer
- Mar 14, 2008
- 21
I am not new to programming but I am new to Visual C++ and the MFC.
I read the example I get the cli versions to work in dos but the problems come when I start to use MFC.
I have never used Classes before and so I read the example and I almost get it to work but not quite in the form.
I know a Class has to be defined before main and so I did, in Cat Class.cpp I have this code:-
// Cat class.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace Catclass;
class Cat
{
public:
int age;
int weight;
};
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Cat Frisky;
Frisky.age=5;
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
MessageBox::Show ("Age="+String::Concat(Frisky.age),"test");
Application::Run(gcnew Form1());
return 0;
}
Voila a MessageBox comes up with Frisky's age, which is 5. No problems there.
But then when I want to access data in the form it's as if Frisky.age does not exist. It won't even compile when I use the same bit of code in Form1.h
-snip-
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
MessageBox::Show (String::Concat(Frisky.age),"test");
}
};
-snip-
I cannot understand why this line works in main but not in Form.h. I am having trouble with MFC. Any help would be appreciated. I declared it as Public: so it should be accessable by all parts of the program surely.
Asimov
I read the example I get the cli versions to work in dos but the problems come when I start to use MFC.
I have never used Classes before and so I read the example and I almost get it to work but not quite in the form.
I know a Class has to be defined before main and so I did, in Cat Class.cpp I have this code:-
// Cat class.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace Catclass;
class Cat
{
public:
int age;
int weight;
};
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Cat Frisky;
Frisky.age=5;
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
MessageBox::Show ("Age="+String::Concat(Frisky.age),"test");
Application::Run(gcnew Form1());
return 0;
}
Voila a MessageBox comes up with Frisky's age, which is 5. No problems there.
But then when I want to access data in the form it's as if Frisky.age does not exist. It won't even compile when I use the same bit of code in Form1.h
-snip-
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
MessageBox::Show (String::Concat(Frisky.age),"test");
}
};
-snip-
I cannot understand why this line works in main but not in Form.h. I am having trouble with MFC. Any help would be appreciated. I declared it as Public: so it should be accessable by all parts of the program surely.
Asimov