Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Right write in of the command

Status
Not open for further replies.

nnnkkk

Programmer
Nov 14, 2009
5
0
0
GB
Hello..
I just started with 2010 c++ builder.

I would like to ask for right write in of the command.
I want to use result from Unit1->Form1 on the Form2.
For exemple,Form1->res1=20,and on the Form2->res4=(res1/res3)
And then
on the Form1= label->Caption=((res4))
I tried meny types of code And i cant get it right.res1 is always = 0 on the Form 2

Thanks for help
 
Where are you declaring res1? One way is to put it in a file that's included in both Form1 and Form2. You wouldn't use Form1->res1 then, just res1.

If you want to use Form1->res1, make sure res1 is a public variable and not private.

The same goes for res4.

Lee
 
Hi.Thanks for reply..res1 I declared on the form1.
Do you mean , should I declace res1 in Project1.cpp file.that include All forms 1,2,3,4.I gonna try it ..Thanks for now
 
I tried to do it those ways but doesnt work.
example:
I declared on the form1 int a,b,c and currency res1,res2,
a=1,b=2,c=3.
res1=a+b+c
and then on the form2 i need to use res1
res3=(res1/res4)*x
Form1->Label1->Caption=((res3)) always showing 0 so res1 on the form2==0.
If both form are public no private how to right declare or write a comand of res1=Form1->res1 or??
thanks for help
 
I think it is better to declare those variables private and use public setters and getters to set and get the value of the variables.

in "Form1.h":

[...]

private:
int a;
int b;
int c;

public:
void __fastcall seta(int q) {a=q;};
int geta() {return a;};

and so on... Think object oriented.

It is my impression you are very new in programming C++ Builder?
 
Hello,thaks for reply.. It is done. But I had a problems
when i declared variables as private.I done it by first way as you advised.I found some errors in the code..And yes Im very new in c++ builder.. thanks
 
nnnkkk wrote:

I declared on the form1 int a,b,c and currency res1,res2,
a=1,b=2,c=3.
res1=a+b+c

It is strange to read this.

in h-file:
...
__published: // IDE-managed Components
private: // User declarations

public: // User declarations
int a;
int b;
int c;
int res1;
int res2;
__fastcall TForm4(TComponent* Owner);
....

in cpp-file:

__fastcall TForm4::TForm4(TComponent* Owner)
: TForm(Owner)

{
// data members of a class cannot be initialized where they
//are declared in the class body. These data members should
//be initialized by the class's constructor, or they can be
//assigned values by "set" function. (C++ how to program
//from Deitel & Deitel)
//This is the error you made.

a=1;
b=2;
C=3;
res1=a+b+c;
}

now because a,b,c,res1 are declared public, the call in Form2: Form1->Label1->Caption=((res3)) won't be 0
 
Please, do not misunderstand me. It is not because you are new in C++ this wasn't a good question. It is a common error and I think many experienced C++ programmers are forgotten why a public declared member that is not initiated correctly can not called outside the class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top