how to use same variable in different procedures?

This is a discussion on how to use same variable in different procedures? within the C++ Programming forums, part of the General Programming Boards category; For example in the Dialog Window Proc I assign some values to an integer variable. Than I need to use ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    26

    how to use same variable in different procedures?

    For example in the Dialog Window Proc I assign some values to an integer variable. Than I need to use this variable in another Proc such as WndProc but it does not recognize the variable in WndProc, says it is not declared. Is there a way to somehow declare a public integer that will be seen in all procedures?

    examaple:

    Code:
    BOOL CALLBACK PieDlgProc(HWND dlghwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    
    int Pval1 = GetDlgItemInt(dlghwnd, txtVal1, 0, FALSE);
    
    }
    Sets a value for Pval1

    Now I need to use this value in WM_PAIT,

    Code:
    case WM_PAINT:
    		{
     AngleArc(ps.hdc, 400, 300, 150, 0, Pval1);
    But I get error that Pval1 is not declared in WM_PAINT.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Make it a global variable. Declare it at the top of the file in global scope.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    If you're going to make it file global, at least put it in an anonymous namespace

    Code:
    namespace
    {
       int g_MayVariable;
    }
    It's considered "good practice" and avoids clashes with other globals that may have the same name. Also locks it to file scope.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    26
    If i make it global and declare at the top than dlghwnd becomes undeclared as it is the HWND of the dialog window.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21