Tips and Tricks for Visual FoxPro - dFPUG

advertisement
TRICKS
Tips and Tricks for
Microsoft Visual FoxPro
German FoxPro User Group
Rainer Becker
Frankfurt/Germany
Rainer Becker
• dFPUG
– Online offers
– Loose Leaf magazine
•
•
•
•
•
VFP-DevCon
Localisation
Framework Visual Extend
MVP, MCP
Wizards & Builders GmbH
Session Topics
•
•
•
•
Visible COM-Servers
Dynamic Constants
User Interface
Dumb Databases
Visible COM-Servers
Business Objects can be turned into
COM-Servers to process business logic
in unattended mode – but you can
create visible COM-servers to…
Main object
• COM-Server need a OLE-Public main object
– It does not have to be a business object
• Very often real business objects are not suited to be called
from outside / other applications (especially via Internet)
– But it has to be an object
• At least a Wrapper-Object is needed
• Based on controls or container class
– Called via „createobject“ from other applications
– Methods/Properties are visible/changeable
– Therefore need to be „protected“
Access and Assign Methods
• Protected Methods / Properties
– Small interface: Hiding Methods / Properites
– Extensive parameterchecking in methods
– Access/Assing-methods for properties
• Assign- & Access-Methods
–
–
–
–
Vartype and valid values (value list, date range, …)
formatting (upper case, spaces, …)
Write-protect properties
Internal properties (hidden property switch)
Additional preparation
• Check startmode
INLIST( _vfp.startmode, 2,3,5 )
• Disable Automation Server Unattended Mode
=SYS(2335,1)
• Integrate error handling
=COMRETURNERROR
• Also from/within Access/Assign-Methods
• Set all your settings
– Old trick: Strg+OK lists all options…
Project definition
• Add main program…
– E.g. use test program for that
• Include metadata tables
• Include Config.fpw
– SCREEN = OFF
(invisible VFP desktop)
– RESOURCE=OFF
(prevent foxuser creation)
– ALLOWEXTERNAL=ON (additional config)
• Turn visible on if/when necessary
Application Execution
• Example in Excel:
–
–
–
–
–
Dim loRef as Object
On Error Goto SomeError
Set loRef = createobject(„exe.obj“)
… other operations
Set loRef = Nothing
• Excel-developer love VFP-based data access, as
it is much easier than ODBC/ADO for them!
– No redefinition of all SQLs if field added/removed
– Problems with transfer of memofields
Application Rollout
• Runtime subdirectory contains
–
–
–
–
<YourServer>.exe
VFP9R.DLL, VFP9RENU.DLL
GDIPLUS.DLL
REGSVR32.EXE
• MSVCR70.DLL in system32
• Regsvr32.exe vfp9r.dll
• <Yourserver>.exe /regserver
Summary COM-Server
• We use WinWord, Excel,
Outlook, Internet Explorer
and other applications by
calling them
• Why dont we offer the same
VISIBLE functionality our
customers?
• Especially Excel-/Worddeveloper/user are an
additional market…
Dynamic Constants
Constants are constantly used to
seldom - and should be more dynamic
anyhow. Underestimated power…
Base knowledge
• #DEFINE CONSTANT Value
– Does not work within „“ or ‚‘
• Different from &, e.g. ?„W&B“ with m.b = value
– But does work with brackets! [Beware]
• #UNDEFINE CONSTANT
• In programs or current method
• #INCLUDE or Class/Form Dialog
– Recursive calls possible
Conditional constants
• #IF Condition (constant or integrated VFP
function at compile/design time)
– #ELSE, #ELIF condition, #ENDIF
• #IFDEF, #IFNDEF constant
– #ELSE, #ENDIF
• Example
– #INCLUDE Foxpro.h / Messagebox
– #IF .F. for comments
– DEFINE … #IF .F. does not work (optimized)
• Except for temporarily disable complete classes
Additional use
• Array dimensions and row / column names
– laTemp( lnCounter, CONST_COL_NAME)
• Translation of strings for localization
– lcMessage = CONT_MSG_TEXT
• Replacement of commands, command lines or conditions
– LOCATE FOR CONT_LOC_COND
• Version dependant functionality
– for VFP-version or own program versions
• Commenting out of SUSPEND, DEBUG …
– #DEFINE SUSPEND *, DEBUG *
• Does not work no more: Commentblock
– See above, #DEFINE BeginComment #IF .F. does not work
Real world examples
• Example 1 / Profiler:
constants for tablenames, value ranges, captions,
colors, classes, assembled multi-part messages
• Example 2 / VFXSync:
Contants for steps /messages, meta table names, meta
table field names, parameter lists, Locate-commands,
logfilenames, table alias
In-Between-Code
•
•
•
•
Contants before and after function calls
#DEFINE BEFORE/AFTER/WITHIN *
#INCLUDE useroverwrite.h
Variables for application status
– For IF/ENDIF-sections
– To change statusvar within code
• Advantage: No calls to empty methods
• More compile-time but faster run-time
Summary Constants
• Configurability drastically
enhanced by using constants
• Only if you really want to
change application you change
the headerfiles
• No Performance-Penalty
compared to all other ways of
configuring application for
special needs
User Interface
Majority of programming time is used
for user interface design and user
interface logic (validations, refresh)
Encapsulation of functions
• Very often a special business object is to
complex and by far to much overhead
• Save functionality in additional button method
and call it via button click
• Save button as class and replace button control
with class for later reuse in other places
• You do not really have to have multi-tiered
architecture – but classes do have big
advantages, especially to separate complex
interactions from form code
Example Addressformater-button
• Powerfull Textmerge()-function can be used in
multiple ways:
–
–
–
–
–
Addressformatter
Addressabreviator (not IntelliSense)
Scripting
End user makros
Mail merge
Example Textmerge
• Format-Memofield contains various <<alias.fieldname>> in correct
position plus hardcoded addtional information like country short
code
SELECT adressformat
LOCATE FOR adressformat.country = lcCountry ;
AND adressformat.active = .T.
IF FOUND()
lcFormat = adressformat.format
lcFormat = TEXTMERGE( lcFormat )
IF NOT EMPTY( lcFormat )
lcMessage = "adress copied to clipboard"
_CLIPTEXT = lcFormat
ENDIF
ENDIF
Summary User Interface
• Do not scatter your code all
over your application
• At least place special
functionality in classes
• It does not have to be a
business object
Dumb Databases
Intelligent database are a problem
sometimes. Why should be make our
programmer lives even more
complicated…
Reasons for stupid databases
• Switching to backend-database is a nightmare if
validations are saved within the database
• Temporarily saving of records is impossible
– Even backups of records have to comply to all rules
• Different record types in the same table makes
validation rules by far to complex
• Rules of user interface and business logic
duplicated in database results in much higher
maintenance costs
– Multiplied by number of backends supported
Synchronization
• Synchronization is only possible in the correct
order of parent/child-relations
• Synchronization only possible between databases with same structure
• … and same level of validation code
• Synchronization example code has been shown
throughout the session
• Will be available as integrated or separate
module for our framework Visual Extend
Summary Databases
• Place only base logic within the
database and no complex
validations
• Application logic should stay
in the application
• Many developers found that it
eases their daily work if the
database is not to intelligent!
• Or use tools like XCASE to
generate database rules
Thank You!
Visit our websites at http://www.dfpug.de,
http://portal.dfpug.de, http://forum.dfpug.de,
http://newsletter.dfpug.de, http://devcon.dfpug.de,
http://roadshow.dfpug.de, http://www.visualextend.com,
http://www.linuxtransfer.de, http://www.visualfoxpro.de
Download