Hi there:

I defined several variables and assigned them values (nonzero) on a form. But I always got zero values of them when I cite them from another form. I am using BCB and never met this problem before even when I was doing the similar things. Can somebody give me hint on this? Thank you in advance!


yeah, I declared them as public ones. Here is how I access them:

in Form1:
int Var1, Var2;
(after a button clicked)
Var1 = 12;
Var2 = 13;

in Form2:
int kid1, kid2;
(after a button clicked)
kid1 = Form1->Var1;
kid2 = Form1->Var2;

But kid1 and kid2 are zero.

Okay. Post the entire code if possible. The root cause of the problem may lie somewhere else rather than in the code you've given.

simply define your variables on form1 and then redefine then with the extern parameter on form2. like this...

int kids = 2, dogs = 1;

extern int kids;
extern int dogs;

Label1->Caption = IntToStr(dogs);
Label2->Caption = IntToStr(kids);

That should solve your issue in BCB2006


Also if you are setting the variables after you make the other form visible, you will not be able to pass the variable until it is reopened...

Like this:
options = 2;
fmAdd->Visible = true;
fmStudent->Visible = false;

Good luck

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