MFC global Variables

Posted on 2000-03-24
Last Modified: 2013-11-20
I've created an SDI application which uses a dialog as the logon screen
. I have another screen which allows the user to change their password.
Is it possible to create a global variable that you can access anywhere within
your program.  I want to store the password the user used to log in
and then display it when they open the changepassword screen. Any help would
be appreciated thanks.
Question by:Yozzer

Accepted Solution

ashish_more earned 50 total points
ID: 2655005
declare the variable as public in your C*App class
CString Password;

you can then access it it other classes by declaring it as an extern variable
extern CString Password.

LVL 23

Expert Comment

ID: 2655008
You can add a static member variable to your CWinApp derived class.

// myapp.h
class CMyApp : public CWinApp
    static CString strPswd;

// myapp.cpp
static CString CMyApp::strPswd = _T("");

// any.cpp
#include "myapp.h"

CMyApp::strPswd = _T("abc");

Expert Comment

ID: 2655009
Yes, you could declare global variable in your .cpp file, but is has its advantages and disadvantages. Advantage is that it could be accessed by any function, and disadvantage is that it might be hard for you to debug your application if you use this variable in too many instances. To be honest with you I never declare global variables, instead I rather design my functions so that I can pass and return variables (values). Why don't you declare that variable as public: in your .h file, rather than having it declared global.
And one more thing - people say global variables in c/c++ don't go along with the spirit of structured and OO programming :) .

Good Luck!

Expert Comment

ID: 2656241
Hi !!
  No need of global variable. Declare right in u r application class. U can access it anywhere using AfxGetApp()->m_MyVariable. where m_MyVariable is decalred in app class.

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question