Click here to Skip to main content
16,005,080 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: HHEEELLLPPPP: global integer! Pin
João Paulo Figueira28-Feb-03 0:40
professionalJoão Paulo Figueira28-Feb-03 0:40 
GeneralRe: HHEEELLLPPPP: global integer! Pin
Sunnygirl28-Feb-03 1:22
Sunnygirl28-Feb-03 1:22 
GeneralRe: HHEEELLLPPPP: global integer! Pin
João Paulo Figueira28-Feb-03 1:29
professionalJoão Paulo Figueira28-Feb-03 1:29 
GeneralRe: HHEEELLLPPPP: global integer! Pin
Sunnygirl28-Feb-03 1:45
Sunnygirl28-Feb-03 1:45 
GeneralRe: HHEEELLLPPPP: global integer! Pin
berndg28-Feb-03 0:45
berndg28-Feb-03 0:45 
GeneralRe: HHEEELLLPPPP: global integer! Pin
Sunnygirl28-Feb-03 1:21
Sunnygirl28-Feb-03 1:21 
GeneralRe: HHEEELLLPPPP: global integer! Pin
berndg28-Feb-03 1:41
berndg28-Feb-03 1:41 
GeneralRe: HHEEELLLPPPP: global integer! Pin
Joan M28-Feb-03 0:48
professionalJoan M28-Feb-03 0:48 
You can do it in several ways:

1.
Declare it in a header file included in all the classes you need.

2.
Declare it in a cpp file and outside the class scope, and then try to reach it with the "extern" keyword.

3.
My favourite one:
In the Application object (typically "TheApp"), declare it inside the class that defines the application, and then reach the application object... (you can be almost sure that you'll be able to reach the header file that contains the application from every header or implementation file in your project.

MoreOver I would declare the variable as private and give the SET/GET interface functions in order to access it (in order to be more standard and to be able to check what is happening always and easily)...

Sample:
header file of the application
class CTCCApp : public CWinApp
{
public:
  CTCCApp();
  ...

private:
  int theIntegerVarToBeAccessed;

public:
  int GetTheIntegerValue();
  void SetTheIntegerValue(int iValue);

implementation file of the application
/*---------------------------------------------
COMMENTS FIRST
1. Initialize your variable at the application's constructor or in initinstance...
2. You'll be able to see that somewhere near top the implementation file there's a line like this one:
CTCCApp TCCApp; (You should substitute "TCC" for your text string) This is your app....
---------------------------------------------*/

// Now the functions that you use to access the number...
// SETTING THE VALUE:
void CTCCApp::SetTheIntegerValue(int iValue)
{
  // Process the value and make whatever you want with it before setting it to your integer variable...
  theIntegerVarToBeAccessed = iValue;
}

// GETTING THE VALUE:
int CTCCApp::GetIntegerValue()
{
  return theIntegerVarToBeAccessed;
}


OK, now it's time to get access to those functions from everywhere you need...

Where you need to access that integer (let's say WHERE.cpp)
/*---------------------------------------------
COMMENTS FIRST
Be sure that you have a #include "TCCApp.h"; in your header or in your implementation file.
---------------------------------------------*/
//Inside the function where you want to access that:
extern CTCCApp TCCApp;
// Now you can get:
TCCApp.GetIntegerValue();
TCCApp.SetIntegerValue(10);


I think that this is the best method because you can "share" a lot of datatypes (even pointers to...) and you can make it in a safe and sorted way...

Hope this helps...
GeneralOpenOffice - API (MailMerging) Pin
MrEyes28-Feb-03 0:02
MrEyes28-Feb-03 0:02 
GeneralRe: OpenOffice - API (MailMerging) Pin
Daniel Turini28-Feb-03 1:45
Daniel Turini28-Feb-03 1:45 
GeneralRe: OpenOffice - API (MailMerging) Pin
MrEyes28-Feb-03 6:22
MrEyes28-Feb-03 6:22 
GeneralDynamically Setting Tab Title on Propertysheet Pin
John Clump27-Feb-03 22:48
John Clump27-Feb-03 22:48 
GeneralRe: Dynamically Setting Tab Title on Propertysheet Pin
Joan M27-Feb-03 23:09
professionalJoan M27-Feb-03 23:09 
GeneralRe: Dynamically Setting Tab Title on Propertysheet Pin
vikramlinux28-Feb-03 0:30
vikramlinux28-Feb-03 0:30 
GeneralSome brain storming. Pin
Tili27-Feb-03 19:49
Tili27-Feb-03 19:49 
GeneralRe: Some brain storming. Pin
Daniel Turini28-Feb-03 1:42
Daniel Turini28-Feb-03 1:42 
GeneralDrag and drop Pin
redlion321027-Feb-03 18:41
redlion321027-Feb-03 18:41 
GeneralRe: Drag and drop Pin
Michael Dunn27-Feb-03 19:07
sitebuilderMichael Dunn27-Feb-03 19:07 
GeneralRe: Drag and drop Pin
redlion321028-Feb-03 0:09
redlion321028-Feb-03 0:09 
GeneralLaunch Mail Client Pin
redlion321027-Feb-03 18:38
redlion321027-Feb-03 18:38 
GeneralRe: Launch Mail Client Pin
Michael Dunn27-Feb-03 19:10
sitebuilderMichael Dunn27-Feb-03 19:10 
GeneralRe: Launch Mail Client Pin
redlion321027-Feb-03 19:28
redlion321027-Feb-03 19:28 
GeneralRe: Launch Mail Client Pin
xxhimanshu27-Feb-03 20:00
xxhimanshu27-Feb-03 20:00 
GeneralRe: Launch Mail Client Pin
redlion321027-Feb-03 22:53
redlion321027-Feb-03 22:53 
GeneralRe: Launch Mail Client Pin
xxhimanshu27-Feb-03 19:37
xxhimanshu27-Feb-03 19:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.