1. Case/switch
- Posted by "SR.Williamson" <writeneu at hotmail.com> Feb 15, 2002
- 435 views
Kat brought up case/switch. I'd love to see this become part of the language. I was writing an artificial life program for Windows. Basically, it was a bunch of buttons in a grid. If a button was "hot", the program would check to see whether the button in the row above and to either side was "hot" to determine whether to make that button itself "hot". Well, as you can imagine, that works fine for the buttons in the middle of the grid. But when you get to the buttons in the grid above, you have to check to see if a button is at a window edge. So there are a lot of "if button1 = leftedge or button2 = rightedge" kind of stuff. It got really tedious. The only think I could think was "This would be so much easier with a Case statement". Is there some easy way to do that that I'm missing?
2. Re: Case/switch
- Posted by Derek Parnell <ddparnell at bigpond.com> Feb 15, 2002
- 443 views
----- Original Message ----- From: "SR.Williamson" <writeneu at hotmail.com> To: "EUforum" <EUforum at topica.com> Subject: Case/switch > > Kat brought up case/switch. I'd love to see this become part of the > language. > > I was writing an artificial life program for Windows. Basically, it was > a bunch of buttons in a grid. If a button was "hot", the program would > check to see whether the button in the row above and to either side was > "hot" to determine whether to make that button itself "hot". > > Well, as you can imagine, that works fine for the buttons in the middle > of the grid. But when you get to the buttons in the grid above, you have > to check to see if a button is at a window edge. So there are a lot of > "if button1 = leftedge or button2 = rightedge" kind of stuff. It got > really tedious. The only think I could think was "This would be so much > easier with a Case statement". > > Is there some easy way to do that that I'm missing? > As a general principle, I try to use a data-oriented programming style. In the case (no pun intended) you describe, you could try something like this... -------------- function doTopLeft( ... ) . . . end function function doTop( ... ) . . . end function function doTopRight( ... ) . . . end function . . . function doMiddle( ... ) . . . end function constant TL = routine_id("doTopLeft"), TP = routine_id("doTop"), TR = routine_id("doTopRight"), . . . MD = routine_id("doMiddle") -- For each button in the grid, you assign it's position handler. -- Assuming you have a sequence holding the button ids and -- another for the position property. sequence GridBtns, GridPosns GridBtns = repeat(0, ButtonCount) GridPosn = repeat(0, ButtonCount) for i = 1 to GridWidth do for j = 1 to GridHeight do if i = 1 then -- top row if j = 1 then x = TL -- top left elsif j = GridHeight then x = BL -- bottom left else x = LF -- left end if elsif i = GridWidth then if j = 1 then x = TR -- top right elsif j = GridHeight then x = BR -- bottom right else x = RT -- right end if else if j = 1 then x = TP -- top elsif j = GridHeight then x = BT -- bottom else x = MD -- middle end if end if -- Calc the index for the position. z = (j-1)* GridWidth + i GridPosn[z] = x next next -- Once this data is initialised, in the click handler do this. procedure onClickBtn() integer b object rc b = find(getSelf(), GridBtns) rc = call_func( GridPosn[b], { . . .}) . . . end procedure ------- The idea is that you do a once-only set up of data that you can quickly use during the running of your code. This is much quicker than making the position decision everytime a button is pressed. This way, you only make that decision once (so long as the button doesn't move). cheers, Derek