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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How declare a variable global to use in two forms?

Status
Not open for further replies.

fayevalentine

Programmer
Jun 2, 2004
38
MX
I want to know if anyone could help me:

I declare a global variable to use in a form, but I cannot access this variable from a diferent form.

How can I do it?

 
Howdy,

The best way to do this is to make the "global" variable a property of the main TForm object. Then you simply include the header file for the main TForm object in the .cpp of the second form, and access it by
MainFormNamedWhatever->The"Global"VariableYouWantAccess

It is all rather simple, but if you need more detailed assistance, just give a haller...

Good luck,
onrdbandit

No! Try not. Do, or do not. There is no try. - Yoda
 
you might try the "extern" thingy

see "extern" in help files

Examples

extern int _fmode;

extern void Factorial(int n);

extern "c" void cfunc(int);

extern

Category

Storage class specifiers

Syntax

extern <data definition> ;

[extern] <function prototype> ;

Description

Use the extern modifier to indicate that the actual storage and initial value of a variable, or body of a function, is defined in a separate source code module. Functions declared with extern are visible throughout all source files in a program, unless you redefine the function as static.

The keyword extern is optional for a function prototype.

Use extern "c" to prevent function names from being mangled in C++ programs.
 
onrdbandit, your approach works for me to bring an EditVariable or a Label, etc. "back" into scope, but how about other variables used in the form class, how can they be brought into scope?
 
It looks like you can also declare the variable(s) in the header file for the form instead of under the event where your main calculations are.
 
you could also declare the variable above
winmain

#include <vcl.h>
#pragma hdrstop
USERES("Project1.res");
USEFORM("Unit1.cpp", Form1);

int GlobalVariable;

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
 
Works best for me if I put the global variable(s)
right after #pragma hdrstop

#include <vcl.h>
#pragma hdrstop
//-------------------------------------------
int GlobalVariable(s);
//--------------------------------------------
USERES("Project1.res");
USEFORM("Unit1.cpp", Form1);



WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
 
kes103 i think u need this for practice and an answer for ure first Q:

you can add your own variables and functions to the header file of your form. as a start do that by adding them to the public part, means you can have direct access to them from another form without that need for a function call.

assuming you have the first form called Form1 and it's headre file Unit1.h

public: // User declarations
__fastcall TForm1(TComponent* Owner);

//add here ure variables
int Unit1Var;

if you want to access this variable from another form, lets say Form2 with Unit2.h as it's header file then in that form include the .h file of Form1:

#include "Unit1.h"

in Form2 (as an example) you can get the variable by:
int Unit2Var = Form1->Unit1Var;
u'll figure out the rest :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top