good evening guys.
I have a problem with my -simple- application.
I have 2 forms (Form1 - Form2), i create an object in Form1 (Person type) and i want to
get the values of data members when i focused to the Form2 and i press getPerson button.
But how can i do this?. I worked with static data and static functions but this is no correct cause if i want to create e vector<Person> person for example i could not to take correct values of my objects.

I attached my project. Thanks for reading :)

Attachments
//---------------------------------------------------------------------------


#pragma hdrstop

#include "person.h"

float Person::height_;
int Person::weight_;

void Person::setHeight(float height){
        height_=height;
}

void Person::setWeight(int weight){
        weight_=weight;
}

float Person::getHeight(){
        return height_;
}

int Person::getWeight(){
        return weight_;
}
//---------------------------------------------------------------------------

#pragma package(smart_init)
//---------------------------------------------------------------------------

#ifndef personH
#define personH
class Person
{
        private:
                static float height_;
                static int weight_;
        public:
                void setHeight(float height);
                void setWeight(int weight);

                static float getHeight();
                static int getWeight();
};

//---------------------------------------------------------------------------
#endif
project.jpeg 15.62 KB
//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"
#include "person.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CreatePersonClick(TObject *Sender)
{
Person person;

person.setHeight(EditHeight->Text.ToDouble());
person.setWeight(EditWeight->Text.ToInt());

Form2->Show();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Mask.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TEdit *EditWeight;
        TButton *CreatePerson;
        TLabel *Label1;
        TLabel *Label2;
        TMaskEdit *EditHeight;
        void __fastcall CreatePersonClick(TObject *Sender);
      
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
#include "person.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::getPersonClick(TObject *Sender)
{
EditHeight->Text=Person::getHeight();
EditWeight->Text=Person::getWeight();

}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published:	// IDE-managed Components
        TEdit *EditWeight;
        TButton *getPerson;
        TLabel *Label1;
        TLabel *Label2;
        TEdit *EditHeight;
        void __fastcall getPersonClick(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif

Static data, also known as class data, is the same for every object of a class. In this case static data members will not help you.

Since you are creating a Person objects in your Form1 ( Unit1.cpp ), you must either:
- pass the Person objects as a reference or a pointer to the Form2 ( Unit2.cpp ).
- store the Person object as a reference or a pointer in a container in a suitable namespace.

Edited 5 Years Ago by Topi Ojala: typo

#ifndef personH
#define personH
class Person
{
        private:
//Try making height_ and weight_ protected instead of private
                static float height_;
                static int weight_;
        public:
                void setHeight(float height);
                void setWeight(int weight);

                static float getHeight();
                static int getWeight();
};
//---------------------------------------------------------------------------
#endif

My problem is that i can not access the object in a different form, when i create this object in form1 for example. The protected data members don't help me, perhaps it helped to use public data members but this is against the philosophy of object oriented programming.

You are correct that you don't want the height, weight and getter/setter methods for them to be static members of your class, or you can't have multiple instances with different values. So you need to fix that first, and then deal with where your Person-instances will live.

First of all, in your click-handler callback method TForm1::CreatePersonClick(), you have created a local instance of class Person, which will be destroyed when control returns from the function. There are a variety of ways to deal with this, depending on how you want to handle your TForm1 -- if the form will be dismissed after you create a person, then you can have your Person instance object be a member of the TForm1 class, and provide a pubic GetPerson() method which returns a reference to it, so TForm2 can call TForm1::GetPerson(); or if you want TForm1 to stay visible and create a new Person from the current values each time the user clicks the button, then (if you want to store the person for further use) you need to add the person into a vector<> or other container, and either pass it as an argument to the constructor of TForm2 when you create the second form, or provide a TForm2::SetPerson() method that your CreatePersonClick() method can call after it creates the TForm2 object.

I can provide you some simple code that does any of these things, but I'm not yet clear what exactly you want your program to do. I think if -you- think through your program clearly, it'll become more clear what needs to be done and where in the code to do it. Give it a try, and then post your revised code with additional specific questions.

Also, since your code is (so far) quite small, please consider including it in your post, with the source of each file labeled and surrounded by [ CODE ] tags, rather than attaching each file separately, so we can more easily scroll through and see what's going on without having to fire up VisualStudio or some external viewer. Thanks!

This article has been dead for over six months. Start a new discussion instead.