我在哪里可以声明类 obj

发布于 2024-09-12 05:21:45 字数 1542 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟的代价 2024-09-19 05:21:45

您要做的就是使 pa 成为表单对象本身的成员。这样所有事件处理程序都可以访问它。

只需从事件处理程序中取出 person p; 并将其放入表单中即可。在 C# 中,您只需将其放在第一个事件处理程序之上 - 我不确定这是否是您在 C++/CLI 中所做的,但值得一试。

person p;

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
    ...
    // Use p here
    p.set( str,int ::Parse(textBox2->Text));
}


private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e)
{
    ...
    // It's still the same p here
    p.get(str,ag);
}

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.

person p;

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
    ...
    // Use p here
    p.set( str,int ::Parse(textBox2->Text));
}


private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e)
{
    ...
    // It's still the same p here
    p.get(str,ag);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文