RodBird
Oct 28, 2008
**Fast FAQ:** These issues were discussed by newcomers on the boards. Have a quick scan evenifif you have programmingexperience.experience. Something will be relevant now or in the nearfuture.future. Hopefully little bells will ring when you are feelingperplexed. I'm going to split this into two parts. Firstlyperplexed. First some general concepts about Liberty and the environment it createsand then error handling. The section on errorand then handlingtacklesthose errore that slip past Liberty's compile time check. **Liberty's programming environment:** **Write Run Compile Execute:** Liberty code is drafted in the IDE and stored as a .bas file. When you click on Runoror Debug, your code is first compiled,anyany coding errors cause the compiler to stopandand report. When error free the compiled code goes to runtime and your instructions are executed and appear on screen in whatever window type you havespecified. So,specified. So, running a program is a two step process, seamless, but still two steps.OptionallyOptionally a .tkn file can be created; it isaa precompiled program that saves a little timewhenwhen starting up. The .tkn also secures your code from prying eyes. **Debug:** The compiler will do some of the debugging for you. Once you get to runtime theLadybugLadybug icon is your best friend.GoGo on, click it. Spend as much time with the debuggerasas the IDE. Click on the >>> animate icon and watch yourprogramprogram run line by line.IfIf it gets a runtime error it stops on the offending line and you can see the content of all your variables at that moment in time. **Window Types:** The first window type you will encounter is Liberty's mainwin. This is the default windowtypetype and is essentially atestingtesting and debugging window. As soon as you are competentyouyou will be using other window types.ItIt is great for your first steps into programming and you can write quite complex programsinin this environment,butbut it has limits. It cannot produce a GUI style interface. It's styled asaa traditional DOS console and cannot producecoloredcolored text. Neither does it support thetraditionaltraditional inkey$() statement, only input$(1). The essential difference is thatprogram flowprogram flow stops and waits for input. You will use the mainwin for development and debugging and a standard Window for almost everything else. A standard window provides GUI controls like listboxes, buttons and checkboxes. There are specialised windows for graphics, text and dialogs. Be aware they exist and take time to choose the correct window type for the task in hand, but have a good reason to move away from the standard window type.You will use the mainwin for development and debugging and a standard Window for almost everything else. A standard window provides GUI controls like listboxes, buttons and checkboxes. There are specialised windows for graphics, text and dialogs. Be aware they exist and take time to choose the correct window type for the task in hand, but have a good reason to move away from the standard window type.**Looping Stop Break Kill:** Endless loops are an endearing feature of all programming languages. A simple loop likeWHILEWHILE WEND can consumeallall of the processor cycles that Liberty can get hold of andifif you don't have dual core your PC will freeze as well as theprogram. Ifprogram. If you are in trouble press CtrlBreak Then navigate to the IDE and from the Menu selectRun,Run, Kill BASIC Programs.DoDo put SCAN inside loops. Scan is a very important command that causes Liberty tocheckcheck for mouse or keyboard input. Don't code delay loops like FOR n=1 TO 50000 : NEXT. Liberty operates in a multithreaded environment where processing cycles are shared. A delay loop like that hogs the processor. Use TIMER if you need a pause, or better still WAIT for the next event.Don't code delay loops like FOR n=1 TO 50000 : NEXT. Liberty operates in a multithreaded environment where processing cycles are shared. A delay loop like that hogs the processor. Use TIMER if you need a pause, or better still WAIT for the next event.**GUI program flow:** Know a bit about BASIC? Cool. To make the transition to GUI you need to have the conceptofof "events" firmly in your mind.YouYou set up events, the keyboard might trigger an event, themousemouse might trigger an event, and the timer might trigger anevent.event. A listbox might triggeranan event. Each event has a handler, a code block that will be called when the event fires. Typically at the end of this code block there will be a WAIT statement. Program executionsitssits and waits here till anothereventevent fires then it shoots off to action that. You may also useSubsSubs to handle events.EventsEvents repeat, you need to switch them off if you don't want them to fire again. The TIMERstatementstatement catches a lot of folksout,out, switch it off immediately it has fired if you want it to beaa one shot event.SubsSubs are self contained and don't know much about the outside world. If an event that has abranchbranch label handler fires whileyouyou are in the sub it's going to fail. Switch off events going intoaa sub or ensure the event is handled by a sub itself. Best nottoto mix subs and branching tolabelslabels in GUI code. **Include:** Include is not relevant to Liberty BASIC. For a start the system is complete in itself and needsnono libraries attached. Thereareare no restrictions on code size so there is no need to page in andoutout segments of code. Code banks are available that letyouyou cut and paste blocks of reusablecodecode directly to the program. The IDE allows you to jump about the program in avarietyvariety ofways. Libertyways. Liberty also provides direct access to Windows API and third party dll files.Typing**Typing & Dimmingvariables:variables:** If you are used to typing variables forget it, this is automated within Liberty. Anything thathashas a fractional value beyond thedecimaldecimal point is stored as a float, anything else is storedasas a long(4Byte)value.AsAs in any computer language you havetoto be aware that comparing two float values isproblematic,problematic, they are never really equal. > or < is ok but don't try and seeif one floatif floatA =another float. UsefloatB. Use Int() or add epsilon if you must.VariablesVariables initiate automatically with 0 or "" values and in general don't need dimmed thoughyouyou can if that’s your habit.ArraysArrays initiate automatically with 10 elements, if you intend to usemoremore than 10 elements you must dimension the array.ArraysArrays are global by default othersmustmust be specified as global.LibertyLiberty can hold a whopping 70Mb of variable data at any one time. Beyond that you needtoto page data in and out from file. **Graphics:** Liberty has a drawing pen, if you don't have that Down you won't see any graphics. So setthethe pen down as the first step inanyany graphics operation.LibertyLiberty has a Flush command that consolidates recent drawing operations into a segment.TheThe segment resides in memoryandand can be redrawn instantly. You must Flush if you wantgraphicsgraphics to stick on screen and not be wiped out if anotherwindowwindow passes over them.YouYou must Flush if you want to print or scale graphics to the printer. Be aware that Flush consumes memory, use it sparingly and delete any segments you don't need. You canuseuse up your 70Mb of memoryinin a split second if you misuse the command. **Sorting:** Liberty sorts in Lexographic or Dictionary order. This is different from ASCII order whichyouyou may be more used to. It ispossiblepossible to arrange for the sort to be in ASCII order just beawareaware of the difference. **Trivia:** If you use the Using(###.##) statement you will get a % in front of your number if it is toobigbig to fit in the template. Theprinterprinter dialog is not able to change from the default printer.GotoGoto [label] needs a command after the label not another [label].KillKill program may leavethethe program in the kill list even though it’s killed. **Liberty Runtime Errors:**A tip,Tip, don't cry "bug”, you may feel foolish later :) try reading this and especially tryrunningrunning animate in the debugger. **OS Error cannot find file.........** while building your .exe Start Liberty from the folder you installed it from to begin creating your .exe **Problem Creating File..............** while building your .exe Don't create your .exe in Liberty's program folder, create it elsewhere. **File System Access Denied..........** Either the file is already open, perhaps you forgot to close it, or it is in use by anotherprogramprogram or you don't have Windowspermissionpermission to access it. Network paths needspecialspecial handling or mapped to a drive: **Error Opening File.................** The file is being written to by another program or, you’re on Vista! join the Userclubclub and get your permissions sorted out.YouYou may need to be an Administratortoto gain permission to access the file. **The file is literally invisible.........** Vista again, get permission. **System Primitive Failed..................** You are out of memory; check what your loops are doing you may be creating averyvery large number or a very (large) smallnumber.number. **Invalid branch label [xyz].........** If it does exist you called it inside a sub that doesn't know where it is, an eventperhaps.perhaps. **Index (nnn) is outside collection bounds ...........** An index is a pointer to a position in an array. Indexes are used in array variables,inin record pointers and by the system inbmps.bmps. Most often you will have asked foranan array item that does not exist. So check what value is inarray(index)array(index), array(index,index) orseek(index)seek(index). **Argument must be a collection......** This is often associated with sending Liberty the wrong set of arguments in acommandcommand string. sending "refresh" tographicboxgraphicbox for example or using $ valueswherewhere numeric values are expected. Animate should stop on the offendingl ine, doubleline, double check the help file for the command and examine the values in the variables. **Collection is empty.................** Liberty expected some arguments but didn't get them, Print "!" to a textbox forexample,example, Liberty expects argumentsafterafter the !command. Animate should stoponon the offending line, double check the help file for the command. **IsEmpty.............................** Liberty found itself without the information it needed.ThisThis is most often program flow.SearchSearch for missing ends, end sub, end if, next. Practice indenting your code so that code blocks are easily visible. Make sure code does not run through to code blocksyouyou didn't expect it to reach.EnsureEnsure Waits are placed where required.OtherOther reasons for this error are missing arguments or missing data, file access denialoror Liberty finding Null infowherewhere it expected something.ThisThis is sometimes the hardest to debug because program flow errors turn up anywherebutbut where the error was made.UseUse animate but don't assume the line shown is the cause.FindFind the error.log and see what fragments of code arementionedmentioned in it.StartStart manual debugging, use step through a line at a time and make sure you are goingwherewhere you expected. Code innoticesnotices or use the run to breakoption. Heyoption in the debugger. Hey debugging is part of the fun.