Board index » cppbuilder » variable in two units

variable in two units


2005-07-16 04:33:01 AM
cppbuilder35
Hi,
How can I use variable witch is declared in one unit, in other units. I made
"include unit hdr" and it doesn't work. Now I have very "inteligent"
solution - I write variable into Edit->Text and it work, but it isn't
proffesional.
Second question is what books do you propose for me to teach myself C++ and
C++ Builder. I use sometimes Turbo C++ and what book ( or websites) is good
to read about using Turbo Vision to make menus and dialogs( or maybe it's
better library for this things)
Thanks in advance, but I'm beginer in C++ and especialy Builder.
 
 

Re:variable in two units

"SebaXYZ" < XXXX@XXXXX.COM >wrote:
Quote

How can I use variable witch is declared in one unit, in
other units.
In the first unit, the variable must be declared as public.
In the second unit, it needs a reference to the first unit. For example (in the second unit):
Form1->Variable1 = SomeThing;
Quote
I made "include unit hdr" and it doesn't work.
Probably because the variable isn't public.
Quote
I write variable into Edit->Text and it work, but it isn't
proffesional.
No kidding.
Quote
what books do you propose for me to teach myself C++ and
C++ Builder.
None. Just dive in and ask qustions in this group or the
vcl.using group. Someone is always available to answer
questions.
~ JD
 

Re:variable in two units

Użytkownik "JD" < XXXX@XXXXX.COM >napisa?w wiadomości
Quote


None. Just dive in and ask qustions in this group or the
vcl.using group. Someone is always available to answer
questions.

~ JD
Thanks a lot
 

{smallsort}

Re:variable in two units

Hi!
"SebaXYZ" < XXXX@XXXXX.COM >wrote in message
Quote
Hi,
How can I use variable witch is declared in one unit, in other units. I
made
"include unit hdr" and it doesn't work. Now I have very "inteligent"
solution - I write variable into Edit->Text and it work, but it isn't
proffesional.
JD suggested a variable in a the main form, but I guess you speak
about namespace scope variables.
Whenever you use the variable it must already be declared, say
extern int SharedVariable; // declaration only
and exactly one translation unit must contain its definition, say
int SharedVariable; // this is both declaration and definition
so its natural to put the declaration into a header file and the definition
to the translation unit where it makes most sense.
(Btw. C++ Builder does this for you with the forms by default:
extern PACKAGE TForm1 *Form1; // declaration in TForm1.h
TForm1 *Form1; // definition in TForm1.cpp
)
Then again, there are very few legitimate reasons to use such a variable,
so maybe you should reconsider not to use it at all. There are many
reasons to avoid namespace scope variables, for instance the unspecified
order of their construction... If you really need a shared variable, google
for "Singleton" (preferably in comp.lang.c++.moderated).
Quote
Second question is what books do you propose for me to teach myself C++
and
C++ Builder. I use sometimes Turbo C++ and what book ( or websites) is
good
to read about using Turbo Vision to make menus and dialogs( or maybe it's
better library for this things)
For starters try Accelerated C++ (www.acceleratedcpp.com/).
I actualy never read that book, but I enjoed reading other (more
advanced) books from the C++ In-Depth Series. You may also
consider reading comp.lang.c++ and comp.lang.c++.moderated
(or alt.comp.lang.learn.c-c++ as the clc++m's FAQ suggests).
Speaking about C++ Builder... why do you do that to you? The one
and only advantage of C++ Builder is its really cute IDE with VCL.
Other than that it's very buggy and outdated (unable to compile
almost any free library). Without a good reason to use it (like your
employer needing to maintain and enhance legacy code) you better
stay away from it. There is a free c++ compiler ("g++") in GCC
(gcc.gnu.org) with Windows port (www.mingw.org/)
and also some free IDEs (www.{*word*76}shed.net/devcpp.html)
though not that cute.
Regards,
Vladimir Marko
 

Re:variable in two units

"SebaXYZ" < XXXX@XXXXX.COM >wrote:
Quote
Second question is what books do you propose for me to teach
myself C++ and C++ Builder.
It helped me to read a book for C++ Builder where I had to go
through a lot of examples. That gave me an understanding on how
the IDE works, what the VCL is and so on. Almost any book of
that kind will do (mine was "C++ Builder 3 in 21 days", should
still be okay, even for BCB 6, for the basics haven't changed).
If you have some understanding of C++, you will easily learn by
doing and by reading these usegroups. If however, you don't
know about classes, constructors, destructors, and public,
protected and private methods and attributes, of pointers and
references, etc., then better get a book on C++, because that
will give you a base to understand.
For instance: If your first question was: how do I pass a
variable to another form, one possible answer would be: change
the second form's constructor's parameter list and pass that
variable when creating it. Or give it a new method where you
pass that variable, e.g. Form2->OpenWithParm(MyParm). Knowing
C++ gives you a lot of techniques to choose from.