help with If . . . command

Just starting out? Need help? Post your questions and find answers here.
Banaticus
User
User
Posts: 21
Joined: Fri Oct 24, 2003 5:49 am
Location: Redlands, CA
Contact:

help with If . . . command

Post 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.
--BX
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: help with If . . . command

Post 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.
Banaticus
User
User
Posts: 21
Joined: Fri Oct 24, 2003 5:49 am
Location: Redlands, CA
Contact:

Post 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?
--BX
Blade
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Aug 06, 2003 2:49 pm
Location: Venice - Italy, Japan when possible.
Contact:

Post by Blade »

Openwindow() could not work (memory problems, wrong params,...), returning 0 (false).

Thanks to the "IF" you can handle this kind of problems.
Banaticus
User
User
Posts: 21
Joined: Fri Oct 24, 2003 5:49 am
Location: Redlands, CA
Contact:

Post 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)?
--BX
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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
--Kale

Image
Banaticus
User
User
Posts: 21
Joined: Fri Oct 24, 2003 5:49 am
Location: Redlands, CA
Contact:

Post 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?
--BX
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post 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.
Post Reply