Dynamic constants
1 / 44

Dynamic Constants - PowerPoint PPT Presentation

  • Uploaded on

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 !.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Dynamic Constants' - elmo-perry

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Dynamic constants

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

Constant Basics

Which commands are available? What do I need to know?

FoxTeach 2001

Basic constant basics
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 Constants


    • 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
Re-DEFINE Constants

  • You can #DEFINE existing const

    • If syntactically identical

    • Otherwise you get an error, so


    • For redefining constants

  • But you can #DEFINE test #DEFINE fun „Joke“ / test

    • ? Fun => „Joke“

FoxTeach 2001

Header files
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
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
Constant-dependent Compilation

  • #IFDEF

    • Depending on existing #DEFINE


    • Depending on no #DEFINE

    • E.g. remark out #DEFINE with *


    • No #ELSEIF

FoxTeach 2001

Basic preprocessor advantages
Basic Preprocessor Advantages


    • 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
Basic Preprocessor Disadvantages


    • 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

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
Messagebox Parameters


  • 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
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
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
Overwrite unwanted Commands


    • Same for DEBUG and other commands not usable in .EXE


    • Use SET ASSERT OFF instead


    • Or define a syntax error ...

FoxTeach 2001

Array dimensions values
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
Version-dependent Functions

  • #IF „0x.00“ $ version()

    • Set library to FoxTools additive

    • Handle other differencesbetween product versions

FoxTeach 2001

Demo beta version
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

Additional Use of Constants

Where else can you use Constants in traditional coding?

FoxTeach 2001

Defines for comment blocks
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:[email protected]

FoxTeach 2001


  • 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
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
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
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

What else to do with Constants?

See another set of possible uses of constants

FoxTeach 2001


  • 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
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
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 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
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
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
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
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
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
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
Solution: In-Between-Hooks

  • Place CONSTANTS before and after each and every functionality


    • Code or call for login functionality


  • 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
Hook Examples

  • Exclude whole functionality


  • 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
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

Material and Updates

Where to go from here ? Where to find additional information ?

FoxTeach 2001

What should not be included
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
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

FoxTeach Web Update Page


This session will NOT have web updates

(except for an updated slideshow)

FoxTeach 2001

Thank you

Thank you!

Please remember to fill out your evaluation.

FoxTeach 2001