473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

where i declare class obj

i have windows form app(Vc++ 2005)
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


Expand|Select|Wrap|Line Numbers
  1. private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
  2.          {person p;
  3.       //string str;
  4.   stringstr(constchar*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(textBox1->Text).ToPointer();
  5.              p.set( str,int ::Parse(textBox2->Text));
  6.  
  7.  
  8. private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e)
  9.          {person p;
  10.           string str;
  11.          int ag;
  12.          p.get(str,ag);
  13.  
  14.  
i declare class as
Expand|Select|Wrap|Line Numbers
  1.  
  2.  class person
  3. {
  4. public:
  5.     person();// create initial value
  6.     ~person();//descon
  7.     void set(string z,int a);//set value
  8.     void get(string &z,int &a);//get value
  9. private:
  10.  
  11.     string name;
  12.     int age ;
  13.  
  14. };
  15.  
------------------------------------------------------------

person cpp
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include "StdAfx.h"
  3. #include "person.h"
  4. #include "stdafx.h"
  5.  
  6. using namespace std;
  7. using namespace System;
  8.     using namespace System::ComponentModel;
  9.     using namespace System::Collections;
  10.     using namespace System::Windows::Forms;
  11.     using namespace System::Data;
  12.     using namespace System::Drawing;
  13.  
  14. person::person()
  15. {name="null";
  16.  age=-1;
  17.  
  18. }
  19. person::~person()
  20. {
  21. }
  22.  
  23. void person::set(string z, int a)
  24. {name=z;
  25. age= a;
  26. }
  27. void person::get(string &z, int &a)
  28. {z =name;
  29. a=age;
  30. }
  31.  
can i upload all solution or not?
Jul 31 '10 #1
1 1239
weaknessforcats
9,208 Expert Mod 8TB
You are using Person as a function argument. That will cause a Person object to be created that will be deleted when the function completes.

What you do is create a Person object before you call your function.

Then do one of two things:
1) pass the address of the object to the function. Here you use a Person* argument
2) pass the object by reference. Here you use a Person& argument.
Jul 31 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hal Vaughan | last post by:
I have a program that consists of only ONE Java class. Is there any way, from within that class, that I can find where the class is on disk? In other words, can I find the path for a loaded...
1
by: Gacu | last post by:
Hi How can I declare a class of a given type - if I have this type in Type object? Thanx gacu@poczta.onet.pl
8
by: StillStuckOnJava | last post by:
I'm using visual studio to create a web application, but I'm having trouble using a few classes that I've defined: public class tank { public tank() { } public float depth; public float...
7
by: rbeyea | last post by:
I am writing a DLL (DLL1) that is wrapping another DLL (DLL2). I need to extract the information returned from DLL2 and pass it out to the Client application this is using DLL1. DLL2 returns...
3
by: Boris | last post by:
After including sstream the compiler reports hundreds of errors. They are all caused by a missing ios_base definition. When I search for ios_base in the installation directory of VS.NET 2003 I find...
2
by: Dave | last post by:
In a Project, if you click the "Project" menu, and then select "Add Class", you get shown all the class templates. Where does this list come from? I used to have Developer Express on my PC, and...
2
by: bluebeta | last post by:
hai, I am a new to embedded visual C++. I have installed embedded visual C++ 4.0 and WinCE standard sdk 420 to do testing. I have created a class call CPerson and add serialize function into...
0
by: Ignacio Machin \( .NET/ C# MVP \) | last post by:
I'm getting an error "c:\inetpub\wwwroot\RGSSO\SO.aspx.cs(363): 'RGSSO.WebForm1.dataSetRG' denotes a 'field' where a 'class' was expected" on the following code: private void...
5
by: Jeremy | last post by:
I've got two projects, both reference system, system.data and system.xml and both include the using System.Xml.Serialization and using System at the top of the file. Both also include the...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.