开发者

where can i declare class obj

开发者 https://www.devze.com 2023-01-09 21:15 出处:网络
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

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:开发者_如何学C: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?


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

精彩评论

暂无评论...
验证码 换一张
取 消