Variable - Run-time variable assignment ======================================= File: BLib.Variable - Update: 1.01 Author: J.G.Harston - Date: 03-Feb-2009 The Variable library provides a generalised function to allow a program to create variables at run-time. Usually a program knows what variable it uses - they are written into the program code itself. Sometimes, though, a program only knows a variable's name when the program itself is run. The most common usage is creating variables from messages files or creating labels in an assembler. The Variable library only works with BASICs that implement RETURN variables in procedure calls. The BBC.Variable library is a version that is compatible with BASICs that do not implement RETURN variables. It modifies itself and must be the last lines of any program it is used in. Assign a value to a variable ============================ DEFPROCVar_Assign(name$,value$) On entry: name$ =name of variable to assign value$=expression to assign to the variable The variable whose name is in name$ is assigned the value in value$. All variable types can be created. Arrays cannot be created with this method. Examples ======== PROCVar_Assign("golden","1.6") is equivalent to golden=1.6 PROCVar_Assign("size%","200") is equivalent to size%=200 PROCVar_Assign("name$","""Jonathan""") is equivalent to name$="Jonathan" PROCVar_Assign("name$","hazel$") is equivalent to name$=hazel$ Using with a messages file ========================== The commonest use of run-time variable assignment is to create program messages with with territory-specific Messages files. Using the following code: in%=OPENIN("Choices:"+AppName$+".Messages") IF in%=0:in%=OPENIN(AppDir$+".Resources."+Territory$+".Messages") IF in%=0:ERROR 214,"No messages file!" REPEAT A$=GET$#in%:IF LEFT$(A$,1)="#":A$="" A%=INSTR(A$,":"):IF A% THEN A%=A%-1:REPEAT:A%=A%+1:UNTIL MID$(A$,A%,1)<>" " PROCVar_Assign("m"+LEFT$(A$,A%-1)+"$",CHR$34+MID$(A$,A%+1)+CHR$34) ENDIF UNTIL EOF#in%:CLOSE#in%:in%=0 With the following Messages file: Info:Inquo File:Fiche Stop:Arret would set up the following variables: mInfo$="Inquo" mFile$="Fiche" mStop$="Arret" Then, instead of a program using: PRINT "Stop" it would use: PRINT mStop$ This allows messages files to be created for whatever language you want the program to communicate with the user with. Version History =============== 1.00 15-Mar-1991 First version written. 1.01 03-Feb-2009 Added '#' and '&' variables, added BBFW crunch directives.