0

I want to declare a global variable inside an MFC application, so that every action inside the application can see this variable and manipulate it, but I don't know where exactly I can declare that variable. in this code i have many button actions, i need to declare a string variable that's shared among this actions.

// CalculatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
 public:
CAboutDlg();

 // Dialog Data
enum { IDD = IDD_ABOUTBOX };
public:
static CString myValue;

protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

  // Implementation
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) {}

void CAboutDlg::DoDataExchange(CDataExchange* pDX){
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CCalculatorDlg dialog


CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCalculatorDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CCalculatorDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON3, &CCalculatorDlg::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON2, &CCalculatorDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON6, &CCalculatorDlg::OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON5, &CCalculatorDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON4, &CCalculatorDlg::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON9, &CCalculatorDlg::OnBnClickedButton9)
ON_BN_CLICKED(IDC_BUTTON8, &CCalculatorDlg::OnBnClickedButton8)
ON_BN_CLICKED(IDC_BUTTON7, &CCalculatorDlg::OnBnClickedButton7)
 END_MESSAGE_MAP()
 // CCalculatorDlg message handlers

     BOOL CCalculatorDlg::OnInitDialog()
    {
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
        pSysMenu->AppendMenu(MF_SEPARATOR);
        pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
}

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);         // Set big icon
SetIcon(m_hIcon, FALSE);        // Set small icon

// TODO: Add extra initialization here

return TRUE;  // return TRUE  unless you set the focus to a control
 }

 void CCalculatorDlg::OnSysCommand(UINT nID, LPARAM lParam)
 {
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
}
else
{
    CDialog::OnSysCommand(nID, lParam);
}
 }

 // If you add a minimize button to your dialog, you will need the code below
 //  to draw the icon.  For MFC applications using the document/view model,
 //  this is automatically done for you by the framework.

void CCalculatorDlg::OnPaint()
{
if (IsIconic())
{
    CPaintDC dc(this); // device context for painting

    SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

    // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2;

    // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
}
else
{
    CDialog::OnPaint();
}
  }

 // The system calls this function to obtain the cursor to display while the user drags
 //  the minimized window.
 HCURSOR CCalculatorDlg::OnQueryDragIcon()
 {
return static_cast<HCURSOR>(m_hIcon);
  }


 void CCalculatorDlg::OnBnClickedButton1()
{
/*
CString s="ABC";
    LPCTSTR str_name = _T("Hello ")*/;
    SetDlgItemText(IDC_EDIT1,_T"9");
/*CAboutDlg::myValue="9";*/
}

void CCalculatorDlg::OnBnClickedButton3(){}

void CCalculatorDlg::OnBnClickedButton2(){}

void CCalculatorDlg::OnBnClickedButton6(){}

void CCalculatorDlg::OnBnClickedButton5(){}

void CCalculatorDlg::OnBnClickedButton4(){}

void CCalculatorDlg::OnBnClickedButton9(){}

void CCalculatorDlg::OnBnClickedButton8(){}

void CCalculatorDlg::OnBnClickedButton7(){}
Eslam Hamdy
  • 7,126
  • 27
  • 105
  • 165
  • exactly how you'd define one in a regular application. – Luchian Grigore Sep 23 '12 at 15:03
  • 2
    What's the specific purpose of the global variable? Avoiding the use of global variables is a common bit of advice handed out to anybody learning to program. - I'd suggest reading and understanding the answers to this question http://stackoverflow.com/questions/484635/are-global-variables-bad?lq=1 then take a step back and look at the alternatives – Ben Cottrell Sep 23 '12 at 15:23
  • i want to declare a string variable that's shared among many actions, – Eslam Hamdy Sep 24 '12 at 14:41

2 Answers2

5

If your project uses precompiled headers (with 99.9999% probability it uses it as default MFC project setting), you can declare this variable in precompiled header file, typically its name is stdafx.h and define it in global scope of any appropriate translation unit (.cpp file). Typically this could be YourProjectName.cpp file that contains application class derived from CWinApp.

// stdafx.h
extern int globalVar; // Global variable declaration, note extern keyword

// YourProjectName.cpp - global scope
int globalVar = 42; // Global variable definition   

Precompiled header stdafx.h is included first in every .cpp file of project, so globalVar will be available everywhere in your project code.

Also would like to mention that global objects are not recommended to be used, it's considered as bad design and anti-pattern.

Community
  • 1
  • 1
Rost
  • 8,779
  • 28
  • 50
1

Put the global variable in a header file and add it to Name Forced Include File.

c_k
  • 1,746
  • 1
  • 20
  • 35