Thread: variables won't change !

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    6

    variables won't change !

    Hi,

    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:
    Code:
    BOOL CALLBACK DialogSettings( HWND dwin, UINT message, WPARAM wParam, LPARAM lParam )
    {
    	switch(message)
    	{
    	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;
    			break;
    		case IDC_3players:
    			//dummy[4] = 3;
    			e = 3;
    			break;
    		case IDC_4players:
    			//dummy[4] = 4;
    			e = 4;
    			break;
    		case IDC_5players:
    			//dummy[4] = 5;
    			e = 4;
    			break;
    		case IDC_TAKELASTWIN:
    			//dummy[0] = TAKELASTWIN;
    			a = TAKELASTWIN;
    			break;
    		case IDC_TAKELASTLOSE:
    			//dummy[0] = TAKELASTLOSE;
    			a = TAKELASTLOSE;
    			break;
    		case IDC_Pfirst:
    			//dummy[2] = YOU;
    			c = YOU;
    			break;
    		case IDC_Compfirst:
    			//dummy[2] = COMP;
    			c = COMP;
    			break;
    		case IDC_Comprand:
    			//dummy[2] = RAND;
    			c = RAND;
    			break;
    		case IDC_P1first:
    			//dummy[3] = P1;
    			d = P1;
    			break;
    		case IDC_P2first:
    			//dummy[3] = P2;
    			d = P2;
    			break;
    		case IDC_P3first:
    			//dummy[3] = P3;
    			d = P3;
    			break;
    		case IDC_P4first:
    			//dummy[3] = P4;
    			d = P4;
    			break;
    		case IDC_P5first:
    			//dummy[3] = P5;
    			d = P5;
    			break;
    		case IDC_Multirand:
    			//dummy[3] = RAND;
    			d = RAND;
    			break;
    		case IDC_Take2:
    			//dummy[1] = TAKE2;
    			b = TAKE2;
    			break;
    		case IDC_Take3:
    			//dummy[1] = TAKE3;
    			b = TAKE3;
    			break;
    		case IDC_Take4:
    			//dummy[1] = TAKE4;
    			b = TAKE4;
    			break;
    		case IDC_Take5:
    			//dummy[1] = TAKE5;
    			b = TAKE5;
    			break;
    
    		case IDOK:
    			//commit changes:
    			gametype[0] = a;
    				//dummy[0];
    			gametype[1] = b;
    			//dummy[1];
    			gametype[2] = c;
    			//dummy[2];
    			gametype[3] = d;
    			//dummy[3];
    			gametype[4] = e;
    			//dummy[4];
    			EndDialog(dwin, IDOK);
    			break;
    
    		case IDCANCEL:
    			EndDialog(dwin, IDCANCEL);
    			break;
    		}
    		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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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
    Location
    near a computer
    Posts
    3,868
    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