Page 1 of 1
help with If . . . command
Posted: Mon Oct 27, 2003 5:57 am
by Banaticus
I see a lot of programs written in PureBasic that have If statements that aren't evaluating anything -- mostly those non-evaluating If statements are related to windows and gadgets.
Code: Select all
If WindowOpen( . . .)
If CreateGadgetList( . . .)
. . .
EndIf
EndIf
I don't understand this syntax. If nothing is being evaluated (no equal to, greater than, not equal to, etc.) why put it in an If statement? Why not type it in just like any other command, by itselt? I mean, it's not proper syntax to say "If Print()" right?
Should all of my commands be imbeded in If statements? Which commands should be in If statements in PureBasic? What is the syntax for the window and gadget commands? After putting an OpenWindow statement in an If statement (for whatever reason) do I need to put any commands that call gadgets in that window before the EndIf?
Thanks, the apprent lack of a reason for those If commands is really confusing.
Re: help with If . . . command
Posted: Mon Oct 27, 2003 6:10 am
by PB
> I don't understand this syntax. If nothing is being evaluated [snip]
Something is being evaluated. If OpenWindow(0,x,...) is just a shorthand
way of doing If OpenWindow(0,x,...)<>0 -- that's all.
Posted: Mon Oct 27, 2003 6:57 am
by Banaticus
So, the mere act of evaluation is enough to run the command OpenWindow()? Can OpenWindow be put byitself, as a normal command instead of inside the If expression? After the EndIf does the window continue to stick around? Or do you have to put all of your code before the EndIf? If the window continues to stick around does it continue to stick around after the program ends?
If I write:
Code: Select all
a = 1
Repeat
a = a + 1
Until OpenWindow()
What is the value of a? What about:
Code: Select all
a = 1
While OpenWindow()
a = a + 1
Wend
What is the value of a? Does the while contine to repeat itself, opening more windows?
Posted: Mon Oct 27, 2003 8:16 am
by Blade
Openwindow() could not work (memory problems, wrong params,...), returning 0 (false).
Thanks to the "IF" you can handle this kind of problems.
Posted: Mon Oct 27, 2003 8:24 am
by Banaticus
Ok, so it's put into an If statement just in case there's problems with the coding. I noticed this, under CreateGadgetList in the help file:
Code: Select all
; Define your window first...
If OpenWindow(0,0,0,250,105,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Create gadgets...")
; Now create the gadget-list...
If CreateGadgetList(WindowID(0)) ; the gadget-list was sucessfully created
; define your gadgets here...
ButtonGadget(0,10,15,230,30,"Test button")
Else ; the gadget-list couldn't be created
; show an error message here, end the program etc...
EndIf
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Does all of a person's code have to go inbetween the Repeat and the Until at the end? Would it make a difference if the Repeat/Until were placed after the second EndIf? Does the window close itself after the second EndIf (the EndIf which refers back to the If in which the window was created)?
Posted: Mon Oct 27, 2003 10:25 am
by blueznl
it's a matter of taste and programming style as well
i don't bother with if in combination with an openwindow.. either it works or it doesn't in which case the success of opening the window is irrelevant as i can't do anything
it's not the proper way to handle it, but i do anything for readable code
in general: purebasic has one thing quite interesting: functions / procedures can return a value, but it's up to you what you do with it
so, while openwindow() returns a value as a functon, in the form of:
retval = openwindow()
you could also opt for
openwindow()
where you just throw awai ('void') the return value
Code: Select all
OpenWindow(0,0,0,250,105,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Create gadgets...")
CreateGadgetList(WindowID(0))
ButtonGadget(0,10,15,230,30,"Test button")
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
finally, the window will close, windows closed it due to a user action and sends you a message
Posted: Mon Oct 27, 2003 1:46 pm
by Kale
i like using:
Code: Select all
Procedure HandleError(result, text.s)
If result = 0 : MessageRequester("Error", text, #PB_MessageRequester_Ok) : End : EndIf
EndProcedure
HandleError(OpenWindow(#WINDOW_CONFIG, 416, 266, 313, 305, #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered , ScreensaverName + " Configuration"), "Configuration window could not be opened.")
HandleError(CreateGadgetList(WindowID(#WINDOW_CONFIG)), "Gadget list could not be created for the configuration dialog box.")
etc...
IIRC i stole the idea from Fred/Bericko
Posted: Mon Oct 27, 2003 9:56 pm
by Banaticus
blueznl wrote:Code: Select all
OpenWindow(0,0,0,250,105,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Create gadgets...")
CreateGadgetList(WindowID(0))
ButtonGadget(0,10,15,230,30,"Test button")
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
And all of your code must be in between the Repeat and the Until, correct? Otherwise, the Repeat/Until loop will continue to repeat itself (and not continue on to the rest of your program) until the window is closed and the condition met, right?
Posted: Mon Oct 27, 2003 10:15 pm
by freedimension
So to say, YES.
Except for the procedures called from within the loop and also except for callback functions (for example assigned with SetWindowCallback()).
Some Code initialising and finalizing the Programm may precede or succed the Repeat...Until-Loop.
I for myself like also the Repeat...ForEver-Loop. Here the program has to be stopped from within the loop. It's your choice.