440 likes | 550 Views
Dynamic Constants. Session V16. Rainer Becker. dFPUG, Germany. Who Am I?. German FoxPro User Group http://www.dfpug.de/forum German VFP DevCon FoxX Professional (4x200 pages) Wizards & Builders GmbH http://www.wizards-builders.com MVP, MCSD, speaker/writer But I am not bilingual !.
E N D
Dynamic Constants Session V16 Rainer Becker dFPUG, Germany FoxTeach 2001
Who Am I? • German FoxPro User Group • http://www.dfpug.de/forum • German VFP DevCon • FoxX Professional (4x200 pages) • Wizards & Builders GmbH • http://www.wizards-builders.com • MVP, MCSD, speaker/writer • But I am not bilingual ! FoxTeach 2001
Constant Basics Which commands are available? What do I need to know? FoxTeach 2001
Basic Constant Basics • All commands with a „#“ in front are pre-processor commands • Those commands are NOT compiled into the final program • Instead they decide • which values are used (#DEFINE) within • which lines are compiled (#IF, #IFDEF) into the final program FoxTeach 2001
DEFINE Constants • #DEFINE CONSTANT Value • Instead of hardcoded values or unchanging repeated variables • Naming conventions: Uppercase • Not working within „ “ or ‚ ‘ • but working within [ ]-brackets • Important for Type-command a.o. FoxTeach 2001
Re-DEFINE Constants • You can #DEFINE existing const • If syntactically identical • Otherwise you get an error, so • #UNDEFINE CONSTANT • For redefining constants • But you can #DEFINE test #DEFINE fun „Joke“ / test • ? Fun => „Joke“ FoxTeach 2001
Header Files • #DEFINES only valid within program (not only current procedure/function) or within current method (not class) • You would have to repeat defines in each • #INCLUDE <file>.h in code • Current directory or SET PATH • Recursive inclusion of header files • „Include“ Dialogue in Form or VCX • Standard Include in Options FoxTeach 2001
Conditional Compilation • #IF <condition> = .T./!=0 • Constants (.T.) • VFP functions (version()) • No variables, functions, API-calls • #ELSE = .F. / = 0 • IF- or ELSE-codeblock compiled • #ELIF <condition> = .T./!=0 • not ELSEIF <g> • #ENDIF FoxTeach 2001
Constant-dependent Compilation • #IFDEF • Depending on existing #DEFINE • #IFNDEF • Depending on no #DEFINE • E.g. remark out #DEFINE with * • #ELSE, #ENDIF • No #ELSEIF FoxTeach 2001
Basic Preprocessor Advantages • #DEFINE • often faster than vars (numeric) • Reduces occurence of repetitive code that needs to be changed later • Increases readability • #IF / #IFDEF • reduces actually compiled code FoxTeach 2001
Basic Preprocessor Disadvantages • #DEFINE • Does not allow parameters so you cannot replace function / method calls (with or without parameters) • #IF / #IFDEF • Does not allow „#FUNCTION“ so you cannot call your own complex decision logic FoxTeach 2001
Classic use of Constants Typical examples in literature and code examples where and how to use constants in Visual FoxPro (additionaly to constant values) FoxTeach 2001
Messagebox Parameters • #INCLUDE FOXPRO.H • Messagebox( „my title“ , MB_ICONEXCLAMATION + MB_SYSTEMMODAL ) • Instead of 48 + 4096 • Should be used generally for function parameters for readability and ease of later changes / ext. FoxTeach 2001
Toolbar Names • See VISUAL CODEBOOK for example of internationalized toolbar names • Can be used for other VFP-window names • Should be used for internal window / toolbar names FoxTeach 2001
Shorten Typed Code • #DEFINE CRLF Chr(10)+chr(13) • Easier to read, less repeating code • #DEFINE PARENT this.parent • Don‘t do it, its unsurvivable - Never overwrite reserved words by accident! • #DEFINE GRANDPARENT this.parent.parent • Shortens reference to grandpa FoxTeach 2001
Overwrite unwanted Commands • #DEFINE SUSPEND * • Same for DEBUG and other commands not usable in .EXE • #DEFINE ASSERT * • Use SET ASSERT OFF instead • #DEFINE WAIT * • Or define a syntax error ... FoxTeach 2001
Array Dimensions / Values • Especially useful for readability:#DEFINEs for array elements • Have you ever converted FP stuff? • Example for ADIR • #DEFINE adir_name 1 • #DEFINE adir_size 2 • #DEFINE adir_date 3 • #DEFINE adir_time 4 • #DEFINE adir_attr 5 FoxTeach 2001
Version-dependent Functions • #IF „0x.00“ $ version() • Set library to FoxTools additive • Handle other differencesbetween product versions FoxTeach 2001
Demo/Beta Version • #DEFINE demoversion .T. • Limit number of records • Disable special functions • #DEFINE betaversion .T. • Limit maximum date of usage • Disable functions if „not implemented yet“ FoxTeach 2001
Additional Use of Constants Where else can you use Constants in traditional coding? FoxTeach 2001
DEFINEs for Comment Blocks • #DEFINE BeginComment #IF .F. • #DEFINE EndComment #ENDIF • Useful if you heavily comment your code • see code documentation guideline for Rational Rose interface • mailto:Rainer.Becker@dfpug.de FoxTeach 2001
Translation • Often seen in frameworks • Strings in menus and programs defined in a header file, label/form captions set by a function • Various practical problems • Works only if source-code provided • Only one language active (therefore useless for web server/multi-user) FoxTeach 2001
Access Methods for Arrays • Variant on parameter passing and array column definition • Usable to translate character parameters passed to an access/assign-method of an array to real offset • See backcolor-example in VFP FoxTeach 2001
Syntax Conversion • Fun from Peter Herzog (inventor of Active FoxPro Pages / AFP) • Replace commands including clauses with compact words (e.g. „bavarian“) • Would be nice in command window • Limited to commands, cannot be used for functions • Might be fun for programmers, but could be a real help for end-users FoxTeach 2001
Scripting Language • Write out memo to file and add lparameters and #include-file • Compile and run with object references as parameters Examples:NEWADDR this.bizobj.datanew()ADDRNAME do setprop with this.bizobj, „adress.name“, FoxTeach 2001
What else to do with Constants? See another set of possible uses of constants FoxTeach 2001
Configuration • Use constants instead of properties for general configuration in case of seldomly changed options • You can change your mind later on the fly by changing the constant to „this.property“ – but not vice versa • Works best with standardized configuration loading mechanism which can be extended FoxTeach 2001
Functional Exclusion • Exclude unneeded functionality with #IF CONSTANT from .h-file Examples: • Call to BackDoor (Steven Black) • Call to installation / update apps • Call to oneways (one-time-install) • Call to config.fpw-creation • Login procedure (if no security) FoxTeach 2001
Performance Measurement • Place speed log code within #IFDEF • Place alternate code (for speed comparison of alternatives) within #IF..#ELSE.. #ENDIF • Place disturbing user interface code within #IFNDEF FoxTeach 2001
Reference Redirection • Reference to global application object with constant • In case you change your mind later • Reference to sub-objects on global application object • Use global and sub-ref in code • Reference to global superclass • Global (datasession-switching) or on form FoxTeach 2001
An Interlude on Superclasses • All GUI elements share behaviour • Init, Gotfocus / lostfocus • Subsets of GUI elements share additional identical behaviour • Lineeditfields like textbox / spinner • Container like Container, column, page • VFP lacks „superclass“ of all GUI elements as well as subclasses • Call superclass w. constant-reference globally / placed on form (datasession) FoxTeach 2001
Reference Revisited • Do not use absolute references between objects at the same tier • Delayed instantiation generates errors • Later integration of additional tier breaks • Never use absolute references to objects on different tiers • Tier might not be available and you need to reference a wrapper instead FoxTeach 2001
Redirective Conversion • Moving procedural code to objects is a pain if you can‘t do it at once with lots of time, maybe faster: • Map public variables to properties • Map function calls to methods • Move to VCX later • Reformat procedure header • Fill „reserved3“ and recompile FoxTeach 2001
Parent Class Redirection • Use DEFINE CLASS <Name> AS <CONSTANT> in classlibs for others • Important for framework integration into larger projects • Allows redirection to own base classes instead of hardcoded hierarchy • Or use “zig-zag subclassing“... • But: Only really useful if working in code, not in visual classlibs... FoxTeach 2001
Addobject Redirection • Use Addobject( <Name>, <CONSTANT>) for aggregation • Important for framework integration into larger projects • Allows redirection of aggregation to own service classes instead of hardcoded hierarchy • See complex concepts for services... • But: Only really useful if working in code, not in classlibs... FoxTeach 2001
Problem: Complex Methods • Changing behavior of parent class in subclass needs high granularity • Even smallest functionality forces a new method for overwriting • Complex functions need admin function to call subfunctions • Change of admin function flow needs rewriting of the whole function • Real problem with framework updates FoxTeach 2001
Solution: In-Between-Hooks • Place CONSTANTS before and after each and every functionality • BEFORE_CALL_LOGIN • Code or call for login functionality • AFTER_CALL_LOGIN • DEFINE constants with asterisk • Include additional empty .h-file • Use a variable for status and placeIF llOk ... ENDIF before / after block FoxTeach 2001
Hook Examples • Exclude whole functionality • BEFORE: #IF .F. / AFTER: #ENDIF • Define execution condition • BEFORE: if this.cond() / AFTER: endif • Use Hooks for changes of running flags or place constant within for flags • WITHIN: llOk = this.myfunc() FoxTeach 2001
General Framework Configuration • #INCLUDE additional custom.h • #UNDEFINE constants in custom.h if changes wanted • Otherwise you get an error! • #DEFINE new value of any kind (function / method-call as you like) • Everyone can configure without interfering with code updates FoxTeach 2001
Material and Updates Where to go from here ? Where to find additional information ? FoxTeach 2001
What should not be included! • As VFP is built in C++ of Visual Studio, it includes - additionally to your header files - the c-header-files if they are on your machine • Beware of duplicates !!! FoxTeach 2001
Additional Material • „Programming the Visual FoxPro Compiler“ by Dennis Hevener in FoxPro Advisor, November 1998 • Various Code examples in the „dFPUG Baseclasses“ in library „VFP solutions“ at www.dfpug.de/forum • See code example of session V17 „Building Server Services“ FoxTeach 2001
FoxTeach Web Update Page www.dbcentral.com This session will NOT have web updates (except for an updated slideshow) FoxTeach 2001
Thank you! Please remember to fill out your evaluation. FoxTeach 2001