troubles with static variables

how do I exactly use static variable? I want to initialize variable "current" at the start of the program and use it later on when user changes combobox value, the code I'm using

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "Header.h"

BOOL CALLBACK TriggerDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_INITDIALOG:
		{
			HWND hCombo = GetDlgItem(hWnd, IDC_COMBO_TRIGGER);
			SendMessage(hCombo, CB_ADDSTRING, NULL, (LPARAM)"Time");
			SendMessage(hCombo, CB_ADDSTRING, NULL, (LPARAM)"Joystick Input");
			SendMessage(hCombo, CB_ADDSTRING, NULL, (LPARAM)"Keyboard Input");
			SendMessage(hCombo, CB_SETCURSEL, 0, NULL);

			static HWND current = GetDlgItem(hWnd, IDC_EDIT_TIME);

			break;
		}
		case WM_DESTROY:
		{
			break;
		}
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_COMBO_TRIGGER:
				{
					if(HIWORD(wParam) == CBN_SELENDOK)
					{
						int sel = SendMessage((HWND)lParam, CB_GETCURSEL, NULL, NULL);
						if(sel == CB_ERR)
							break;

						static HWND current;
						string help[] = { "milliseconds", "Joystick Trigger", "Keyboard Button" };
						SendMessage(GetDlgItem(hWnd, IDC_INPUT_HELP), WM_SETTEXT, NULL, (LPARAM)help[sel].c_str());

						if(current == NULL)
							MessageBox(hWnd, "null", NULL, NULL);

						if(sel == 0)
						{
							//time, show editbox for milliseconds
							ShowWindow(GetDlgItem(hWnd, IDC_EDIT_TIME), TRUE);
						}
						else if(sel == 1)
						{
							//joystick, show combobox with available joystick triggers
						}
						else if(sel == 2)
						{
							//keyboard, process keyboard input
						}
					}
					break;
				}
			}
			break;
		}
	}
	return FALSE;
}


however I actually am getting the "null" message box as the "current" value is NULL for some reason, I never really worked with static variables but shouldn't it store handle to the IDC_EDIT_TIME control?
Last edited on
You define current twice (two separate variables). Both variables are only accessible within their blocks. Hence current on line 35 will never change.
Try moving line 35 to line 4 (just within the first brackets). This will declare the variable for as long as your program is between the brackets of line 4 and line 63.

Then if you want to change the value of this variable in line 15, instead of
"static HWND current = " just use "current =".

After the variable has been declared (I suggested to do this in line 4), you don't have to tell the system what kind of variable it is anymore, you just mention its name and its new value.

The "static" part doesn't really have anything to do with this, this is just assigning a value to a variable. But I am curious why you would like to use a static variable here.

Kind regards, Nico
Last edited on
@coder777 but isn't this how static is supposed to work? to have one variable with same value within multiple blocks? http://www.cprogramming.com/tutorial/statickeyword.html look at the loop, did I understand it incorrectly?

@Nico I want something else than a global variable to hold the handle of the current control that's being used (for time, joystick or keyboard) as the user can change them whenever they want, so basically I'm just looking for a replacement for global variable
In your case you have two variable with the name current within two different blocks. They are not related and occupy two different memory locations. If you want a single variable then define only a single variable and place it where it can be used by all blocks which require it. In your case (like Nico said) place it before the switch block on line 5.


look at the loop, did I understand it incorrectly?
There is only one static variable defined within that loop while in your code there are two: line 14 and line 35 (which are unrelated).
Topic archived. No new replies allowed.