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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

690 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