Board index » cppbuilder » Accessing variable from another form

Accessing variable from another form


2005-10-15 04:54:41 AM
cppbuilder25
Hi,
i declared a variable, (extern int row = 0) on my main form and i wanna use
it in my OTHER forms, shortly i wanna call it from another form, is there a
way to do it?
Thanks
 
 

Re:Accessing variable from another form

If the variable is declared in Unit1.h and you want to use it in Unit2.cpp
for example
add
#include "Unit1.h"
into
Unit2.cpp
Analian
 

Re:Accessing variable from another form

Sorry but it doesn't work!!!
"Analian" < XXXX@XXXXX.COM >, haber iletisinde žunlar?
yazd?43501b17$ XXXX@XXXXX.COM ...
Quote
If the variable is declared in Unit1.h and you want to use it in Unit2.cpp
for example
add
#include "Unit1.h"
into
Unit2.cpp

Analian


 

{smallsort}

Re:Accessing variable from another form

How to declare in .h???
"Analian" < XXXX@XXXXX.COM >, haber iletisinde žunlar?
yazd?43501b17$ XXXX@XXXXX.COM ...
Quote
If the variable is declared in Unit1.h and you want to use it in Unit2.cpp
for example
add
#include "Unit1.h"
into
Unit2.cpp

Analian


 

Re:Accessing variable from another form

1- You cannot "call" a variable.
2- An extern declaration says something is
declared elsewhere. The item still needs to
be declared.
Add the following to the private section of TForm1
int row;
Add the following to the public section of TForm1
int Row() const { return row; }
Code in functions in another form or code not part of any class can read the
value of the variable as Form1->Row()
If you have a need for the others to be able to alter the value of the
variable then add this to the public section of TForm1
void Row(int new_value) { row = new_value; }
And they can alter the value with a line like this:
Form1->Row(7); // sets the value of "row" to 7
Note that any source file which uses this would need to include the header
file in which class TForm1 is defined.
. Ed
Quote
Cenk wrote in message
news: XXXX@XXXXX.COM ...
Sorry but it doesn't work!!!

"Analian" < XXXX@XXXXX.COM >, haber iletisinde žunlar?
yazd?43501b17$ XXXX@XXXXX.COM ...
>If the variable is declared in Unit1.h and you want to use it in
>Unit2.cpp for example
>add
>#include "Unit1.h"
>into
>Unit2.cpp
 

Re:Accessing variable from another form

Cenk wrote:
Quote
i declared a variable, (extern int row = 0) on my main form
What do you mean with 'on my form' ? Is it a private or a public
member of the formdefinition ? Or did you make it global?
Show what you have in your .cpp file and what in the .h file that
you include in other .ccp files.
What erors are there ? What do you see ?
Hans.
 

Re:Accessing variable from another form

Actually i aint declared variable in .h file only declared variable in cpp
file.
Shortly, i wanna declare a global variable to use every forms.
"Hans Galema" < XXXX@XXXXX.COM >, haber iletisinde sunlari
yazdi:435024d9$ XXXX@XXXXX.COM ...
Quote
Cenk wrote:

>i declared a variable, (extern int row = 0) on my main form

What do you mean with 'on my form' ? Is it a private or a public
member of the formdefinition ? Or did you make it global?

Show what you have in your .cpp file and what in the .h file that
you include in other .ccp files.

What erors are there ? What do you see ?

Hans.
 

Re:Accessing variable from another form

On Sat, 15 Oct 2005 00:03:48 +0300, Cenk wrote:
Quote
Sorry but it doesn't work!!!
How doesn't it work? please be precise
here's an untested example of extern.
// a.h
#ifndef AH_
extern int Thing;
int foo(int x);
#endif
|| a.cpp
#include <iostream>
#include "a.h"
int Thing = 2;
int foo(int x)
{
Thing += x;
std::cout << "foo(): x = " << x << " Thing = thing" << std::endl;
return Thing;
}
// b.cpp
#include <iostream>
#include "a.h"
int bar(int x)
{
Thing *= x;
std::cout << "bar(): x = " << x << " Thing = thing" << std::endl;
return Thing;
}
int main()
{
foo(3);
bar(4);
return 0;
}
--
liz
 

Re:Accessing variable from another form

On Sat, 15 Oct 2005 00:44:43 +0300, Cenk wrote:
Quote
Actually i aint declared variable in .h file only declared variable in cpp
file.
Shortly, i wanna declare a global variable to use every forms.
While you almost certainly /don't/ want to do this the way to do so is
to put the extern declaration (extern int thing;) in one .h file, and
put the variable definition (int thing;) in one .cpp file. Include
the .h file in all .cpp files that use your global variable.
what you probably want is a property that multiple forms monitor, and
one maintains.
Additionally, please trim your quotes.
--
liz