Session V 16 Dynamic Constants Rainer Becker d

Session V 16 Dynamic Constants Rainer Becker d. FPUG, Germany Fox. Teach 2001

Who Am I? • German Fox. Pro User Group http: //www. dfpug. de/forum – German VFP Dev. Con – Fox. X Professional (4 x 200 pages) • Wizards & Builders Gmb. H http: //www. wizards-builders. com • MVP, MCSD, speaker/writer – But I am not bilingual ! Fox. Teach 2001

Constant Basics Which commands are available? What do I need to know? Fox. Teach 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 Fox. Teach 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. Fox. Teach 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“ Fox. Teach 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 Fox. Teach 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 Fox. Teach 2001

Constant-dependent Compilation • #IFDEF – Depending on existing #DEFINE • #IFNDEF – Depending on no #DEFINE – E. g. remark out #DEFINE with * • #ELSE, #ENDIF – No #ELSEIF Fox. Teach 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 Fox. Teach 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 Fox. Teach 2001

Classic use of Constants Typical examples in literature and code examples where and how to use constants in Visual Fox. Pro (additionaly to constant values) Fox. Teach 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. Fox. Teach 2001

Toolbar Names • See VISUAL CODEBOOK for example of internationalized toolbar names • Can be used for other VFPwindow names • Should be used for internal window / toolbar names Fox. Teach 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 – Shortens reference to grandpa Fox. Teach 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. . . Fox. Teach 2001

Array Dimensions / Values • Especially useful for readability: #DEFINEs for array elements – Have you ever converted FP stuff? • Example for ADIR – – – #DEFINE adir_name #DEFINE adir_size #DEFINE adir_date #DEFINE adir_time #DEFINE adir_attr 1 2 3 4 5 Fox. Teach 2001

Version-dependent Functions • #IF „ 0 x. 00“ $ version() – Set library to Fox. Tools additive – Handle other differences between product versions Fox. Teach 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“ Fox. Teach 2001

Additional Use of Constants Where else can you use Constants in traditional coding? Fox. Teach 2001

DEFINEs for Comment Blocks • #DEFINE Begin. Comment #IF. F. • #DEFINE End. Comment #ENDIF • Useful if you heavily comment your code – see code documentation guideline for Rational Rose interface – mailto: Rainer. Becker@dfpug. de Fox. Teach 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) Fox. Teach 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 Fox. Teach 2001

Syntax Conversion • Fun from Peter Herzog (inventor of Active Fox. Pro 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 Fox. Teach 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“, Fox. Teach 2001

What else to do with Constants? See another set of possible uses of constants Fox. Teach 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 Fox. Teach 2001

Functional Exclusion • Exclude unneeded functionality with #IF CONSTANT from. h-file Examples: – Call to Back. Door (Steven Black) – Call to installation / update apps – Call to oneways (one-time-install) – Call to config. fpw-creation – Login procedure (if no security) Fox. Teach 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 Fox. Teach 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 Fox. Teach 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) Fox. Teach 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 Fox. Teach 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 „reserved 3“ and recompile Fox. Teach 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. . . Fox. Teach 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. . . Fox. Teach 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 Fox. Teach 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 place IF ll. Ok. . . ENDIF before / after block Fox. Teach 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: ll. Ok = this. myfunc() Fox. Teach 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 Fox. Teach 2001

Material and Updates Where to go from here ? Where to find additional information ? Fox. Teach 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 !!! Fox. Teach 2001

Additional Material • „Programming the Visual Fox. Pro Compiler“ by Dennis Hevener in Fox. Pro Advisor, November 1998 • Various Code examples in the „d. FPUG Baseclasses“ in library „VFP solutions“ at www. dfpug. de/forum • See code example of session V 17 „Building Server Services“ Fox. Teach 2001

Fox. Teach Web Update Page www. dbcentral. com This session will NOT have web updates (except for an updated slideshow) Fox. Teach 2001

Thank you! Please remember to fill out your evaluation. Fox. Teach 2001
- Slides: 44