Thread: variables won't change !

  1. #1
    Registered User
    Join Date
    Oct 2010

    variables won't change !


    On running debug I found out that some variables weren't changing when explicitly asked to, i.e. int a = 5. They are inside a dialog box function.

    gametype is a global variable (since I never figured out how to pass an array into a dialog box - if any one knows how to do that I'd be glad to hear it!) and dummy is a local one. Neither dummy[0] = something nor gametype[0] = dummy[0] work. gametype[0] stays as what it was before and dummy[0] stays as a random huge negative number.

    So what's happening?

    On a related note, some child window buttons work fine until this dialog is called, then they won't work anymore (they look enabled, but can't be clicked). Does anyone know why that might happen?

    Anyway, here is the code for the dialog box function:
    BOOL CALLBACK DialogSettings( HWND dwin, UINT message, WPARAM wParam, LPARAM lParam )
    	case WM_INITDIALOG:
    		//make dummy variable(for if user changes their mind about settings)
    		int a, b, c, d, e;
    		//int dummy[5];
    	//set initial checks
    	int radioID;
    	if(gametype[0] == TAKELASTLOSE) radioID = IDC_TAKELASTLOSE;
    	if(gametype[0] == TAKELASTWIN) radioID = IDC_TAKELASTWIN;
    	CheckRadioButton(dwin, IDC_TAKELASTLOSE, IDC_TAKELASTWIN, radioID);
    	if(gametype[1] == TAKE2) radioID = IDC_Take2;
    	if(gametype[1] == TAKE3) radioID = IDC_Take3;
    	if(gametype[1] == TAKE4) radioID = IDC_Take4;
    	if(gametype[1] == TAKE5) radioID = IDC_Take5;
    	CheckRadioButton(dwin, IDC_Take2, IDC_Take5, radioID);
    	if(gametype[2] == YOU) radioID = IDC_Pfirst;
    	if(gametype[2] == COMP) radioID = IDC_Compfirst;
    	if(gametype[2] == RAND) radioID = IDC_Comprand;
    	CheckRadioButton(dwin, IDC_Pfirst, IDC_Comprand, radioID);
    	if(gametype[3] == P1) radioID = IDC_P1first;
    	if(gametype[3] == P2) radioID = IDC_P2first;
    	if(gametype[3] == P3) radioID = IDC_P3first;
    	if(gametype[3] == P4) radioID = IDC_P4first;
    	if(gametype[3] == P5) radioID = IDC_P5first;
    	if(gametype[3] == RAND) radioID = IDC_Multirand;
    	CheckRadioButton(dwin, IDC_P1first, IDC_Multirand, radioID);
    	if(gametype[4] == 2) radioID = IDC_2players;
    	if(gametype[4] == 3) radioID = IDC_3players;
    	if(gametype[4] == 4) radioID = IDC_4players;
    	if(gametype[4] == 5) radioID = IDC_5players;
    	CheckRadioButton(dwin, IDC_2players, IDC_5players, radioID);
    	return TRUE;
    	case WM_COMMAND:
    		switch LOWORD(wParam)
    		case IDC_2players:
    			//dummy[4] = 2;
    			e = 2;
    		case IDC_3players:
    			//dummy[4] = 3;
    			e = 3;
    		case IDC_4players:
    			//dummy[4] = 4;
    			e = 4;
    		case IDC_5players:
    			//dummy[4] = 5;
    			e = 4;
    			//dummy[0] = TAKELASTWIN;
    			a = TAKELASTWIN;
    			//dummy[0] = TAKELASTLOSE;
    			a = TAKELASTLOSE;
    		case IDC_Pfirst:
    			//dummy[2] = YOU;
    			c = YOU;
    		case IDC_Compfirst:
    			//dummy[2] = COMP;
    			c = COMP;
    		case IDC_Comprand:
    			//dummy[2] = RAND;
    			c = RAND;
    		case IDC_P1first:
    			//dummy[3] = P1;
    			d = P1;
    		case IDC_P2first:
    			//dummy[3] = P2;
    			d = P2;
    		case IDC_P3first:
    			//dummy[3] = P3;
    			d = P3;
    		case IDC_P4first:
    			//dummy[3] = P4;
    			d = P4;
    		case IDC_P5first:
    			//dummy[3] = P5;
    			d = P5;
    		case IDC_Multirand:
    			//dummy[3] = RAND;
    			d = RAND;
    		case IDC_Take2:
    			//dummy[1] = TAKE2;
    			b = TAKE2;
    		case IDC_Take3:
    			//dummy[1] = TAKE3;
    			b = TAKE3;
    		case IDC_Take4:
    			//dummy[1] = TAKE4;
    			b = TAKE4;
    		case IDC_Take5:
    			//dummy[1] = TAKE5;
    			b = TAKE5;
    		case IDOK:
    			//commit changes:
    			gametype[0] = a;
    			gametype[1] = b;
    			gametype[2] = c;
    			gametype[3] = d;
    			gametype[4] = e;
    			EndDialog(dwin, IDOK);
    		case IDCANCEL:
    			EndDialog(dwin, IDCANCEL);
    		return true;
    	return false;
    If anyone has an idea of why the buttons might not work I will post the code of WM_COMMAND where the buttons are processed and the dialog is called.

    Thanks heaps in advance, it is puzzling me incredibly!!

  2. #2
    Join Date
    Aug 2010
    Ontario Canada
    On the matter of passing an array into a dialog box... use DialogBoxParam and set the LPARAM value to point at your struct or array. You will need to typecast it as (LPARAM) &ARRAY to get it in there, then inside the dialog in WM_INITDIALOG convert it back to a pointer and store it as a page-global variable... PARRAY = (ARRAY*) LPARAM

  3. #3
    train spotter
    Join Date
    Aug 2001
    near a computer
    Your issue is 'scope' of your variables (how long and which sections of code they exist in).
    Callbacks process all the msgs, each as a seperate event. Each msg creates a 'new' copy of the callback function (and cleans it up when it finishes).

    When the callback gets a WM_INIT msg the variables get a value random value (because you did not initialise them).

    You use the variable and assign a new value.

    When the WM_INIT msg finishes the variables (and any new value) go out of scope (are freed/deleted).

    When the callback gets a WM_COMMAND msg the variables again get a random value.

    Your comparison fails as the old value has been lost.

    The solution is to use 'static' variables for any that you want to retain their value for the life of the app (rather than the life of the msg).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execve() accepting & using environment variables?
    By phummon in forum C Programming
    Replies: 1
    Last Post: 09-25-2010, 12:39 PM
  2. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM
  3. Replies: 2
    Last Post: 11-08-2002, 03:22 AM
  4. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM