我在哪里可以声明类 obj
我有 Windows 窗体应用程序 2 文本框1,2 2 按钮1,2 我有类 person {有 get, set (string ,int)} 我想声明 人 p; 在两个按钮1,2中看到它
我的问题是我声明 人 p;当按钮结束时在按钮内部 p.~person();被称为 所以我无法保存值
,当我调用 p.get 时,我得到初始值
代码,
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{person p;
//string str;
stringstr(constchar*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(textBox1->Text).ToPointer();
p.set( str,int ::Parse(textBox2->Text));
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{person p;
string str;
int ag;
p.get(str,ag);
我声明类,因为
class person
{
public:
person();// create initial value
~person();//descon
void set(string z,int a);//set value
void get(string &z,int &a);//get value
private:
string name;
int age ;
};
------------------------------------------------------------
person cpp
#include "StdAfx.h"
#include "person.h"
#include "stdafx.h"
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
person::person()
{name="null";
age=-1;
}
person::~person()
{
}
void person::set(string z, int a)
{name=z;
age= a;
}
void person::get(string &z, int &a)
{z =name;
a=age;
}
我可以上传所有解决方案吗?
i have windows form app
2 text box1,2
2 button1,2
i have class person {have get, set (string ,int)}
and i want to declare
person p;
to see it in both two push button1,2
my problem is i declare
person p; inside buttons when buttons end
p.~person();is called
so i cant save value
and when i call p.get i get initial value
code
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{person p;
//string str;
stringstr(constchar*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(textBox1->Text).ToPointer();
p.set( str,int ::Parse(textBox2->Text));
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{person p;
string str;
int ag;
p.get(str,ag);
i declare class as
class person
{
public:
person();// create initial value
~person();//descon
void set(string z,int a);//set value
void get(string &z,int &a);//get value
private:
string name;
int age ;
};
------------------------------------------------------------
person cpp
#include "StdAfx.h"
#include "person.h"
#include "stdafx.h"
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
person::person()
{name="null";
age=-1;
}
person::~person()
{
}
void person::set(string z, int a)
{name=z;
age= a;
}
void person::get(string &z, int &a)
{z =name;
a=age;
}
can i upload all solution or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要做的就是使 pa 成为表单对象本身的成员。这样所有事件处理程序都可以访问它。
只需从事件处理程序中取出
person p;
并将其放入表单中即可。在 C# 中,您只需将其放在第一个事件处理程序之上 - 我不确定这是否是您在 C++/CLI 中所做的,但值得一试。What you want to do is make p a member of the form object itself. That way it will be accessible to all the event handlers.
Just take
person p;
out of your event handler and put it in your form. In C# you would just put it above the first event handler--I'm not certain that's what you do in C++/CLI, but it's worth a shot.