Advanced Object Oriented
Programming with
Visual FoxPro 6.0
Markus Egger
Hentzenwerke Publishing
Published by:
Hentzenwerke Publishing
980 East Circle Drive
Whitefish Bay WI 53217 USA
Hentzenwerke Publishing books are available through booksellers and directly from the
publisher. Contact Hentzenwerke Publishing at:
414.332.9876
414.332.9463 (fax)
www.hentzenwerke.com
Advanced Object Oriented Programming with Visual FoxPro 6.0
By Markus Egger
Technical Editor: Mac Rubel
Copy Editor: Jeana Randell
Copyright © 1999 by Markus Egger
All other products and services identified throughout this book are trademarks or registered
trademarks of their respective companies. They are used throughout this book in editorial
fashion only and for the benefit of such companies. No such uses, or the use of any trade
name, is intended to convey endorsement or other affiliation with this book.
All rights reserved. No part of this book, or the ebook files available by download from
Hentzenwerke Publishing, may be reproduced or transmitted in any form or by any means,
electronic, mechanical photocopying, recording, or otherwise, without the prior written
permission of the publisher, except that program listings and sample code files may be entered,
stored and executed in a computer system.
The information and material contained in this book are provided “as is,” without warranty of
any kind, express or implied, including without limitation any warranty concerning the
accuracy, adequacy, or completeness of such information or material or the results to be
obtained from using such information or material. Neither Hentzenwerke Publishing nor the
authors or editors shall be responsible for any claims attributable to errors, omissions, or other
inaccuracies in the information or material contained in this book. In no event shall
Hentzenwerke Publishing or the authors or editors be liable for direct, indirect, special,
incidental, or consequential damages arising out of the use of such information or material.
ISBN: 0-96550-938-9
Manufactured in the United States of America.
Dedication
Ich widme dieses Buch meinen Eltern (Erna und Franz Egger)
sowie meinen Großeltern (Anna und Franz Wimmer, Hildegard und Josef Egger).
v
List of Chapters
SECTION 1—Basic Concepts 1
Chapter 1: Basic Concepts 3
Chapter 2: How Things Are Done in Visual FoxPro 31
Chapter 3: What's Not So Obvious 59
Chapter 4: Using Shrink-Wrapped Classes 69
Chapter 5: OOP Standard Tools 177
Chapter 6: Good Habits 237
Chapter 7: Evaluating Risks and Benefits 251
SECTION 2—Advanced Object-Oriented Programming 259
Chapter 8: The Bigger Picture 261
Chapter 9: Three-Tiered Development 271
Chapter 10: Patterns 281
Chapter 11: Object Metrics 295
SECTION 3—Object Modeling 313
Chapter 12: The Unified Modeling Language 315
Chapter 13: Collecting Requirements 347
Chapter 14: Analyzing the Problem 361
Chapter 15: Object Modeling 377
Chapter 16: Implementation 397
Chapter 17: Documenting Existing Projects 411
vii
Table of Contents
Acknowledgements
xvii
Foreword
xix
SECTION 1—Basic Concepts 1
Chapter 1: Basic Concepts 3
Encapsulation 3
Classes 3
Properties 4
Methods 5
How to draw classes 6
Inheritance 6
Overwriting code 7
Programming by exception 8
What about multiple inheritance? 10
Base classes—a first look 11
How to draw subclassing 11
Polymorphism 12
Messages 13
Events 13
Events vs. event methods 14
Access and assign methods 14
Composition 18
Containers 19
Composition by reference 21
Abstraction 23
Building class hierarchies 23
Hiding information 24
Interfaces 27
Chapter 2: How Things Are Done in Visual FoxPro 31
Instantiating classes 31
Constructors and destructors 31
CreateObject() 34
NewObject() 34
.AddObject() and .NewObject() 35
Passing parameters to objects 35
SCATTER… NAME… 36
viii
Object references 36
Object lifetime 37
THIS, THISFORM, THISFORMSET and Parent 38
Variable references 39
Property references 40
Cleaning up after yourself 40
Differences between classes and forms 41
Instantiating forms 42
DataEnvironment 45
Inheritance 45
Combining form classes and forms 45
Templates 46
Visual FoxPro base classes 47
NODEFAULT 47
Custom classes 47
Control classes 48
Adding properties on the fly 49
Should you, or shouldn't you? 49
Collections 50
FoxPro's collections 51
The collections you create yourself 51
Collections provided by others 56
Features you might miss in Visual FoxPro 56
Multiple inheritance revisited 56
Friendship relations 57
Static properties 57
Operator overloading 57
Copying objects 58
Chapter 3: What's Not So Obvious 59
Instance programming and pseudo-subclassing 59
Instance programming 59
Pseudo-subclassing 61
Visual vs. non-visual classes 62
Advantages of non-visual classes 63
Why not go the visual route? 63
Visual classes: Nintendo for adults? 65
Some classes are non-visual only 66
Creating your own set of base classes 67
Some suggestions 68
ix
Chapter 4: Using Shrink-Wrapped Classes 69
Reuse what others create 69
What is available? 70
Black box objects 70
White box objects 72
COM components 72
Class libraries 76
Frameworks 77
Complimentary tools 78
The Fox Foundation Classes 78
Class overview 79
Application 79
Data Session Manager 79
Error Object 82
Object State 85
System Toolbar 86
Trace Aware Timer 86
Window Handler 87
Automation 87
Cross Tab 88
Graph by Record 89
Graph Object 92
Mail Merge Object 93
Pivot Table 95
Data editing 97
Data Edit Buttons 97
Data Validation 97
Offline Switch 98
Simple Edit Buttons 99
Data navigation 100
Data Navigation Buttons 100
Data Navigation Object 101
GoTo Dialog Box 101
GoTo Dialog Box Button 102
GoTo Spinner 102
Navigation Toolbar (Container) 102
Navigation Toolbar (Toolbar) 103
Simple Picture Navigation Buttons 103
Simple Navigation Buttons 104
VCR Buttons 104
VCR Picture Navigation Buttons 104
Data query 105
Conflict Catcher 105
Distinct Values Combo 106
x
Filter Dialog Box 106
Filter Dialog Box Button 109
Filter Expression Dialog Box 109
Find Button 110
Find Dialog Box 111
Find (Findnext) Buttons 112
Find Object 113
Locate Button 114
Lookup Combobox 114
QBF (query-by-form) 116
Sort Dialog Box 117
Sort Dialog Box Button 118
Sort Object 119
Sort Selector 119
SQL Pass Through 120
Data utilities 121
Array Handler 121
String Library 122
Date time 123
ActiveX Calendar 123
Clock 124
Stop Watch 124
File utilities 125
File Version 125
Find Files/Text 126
Type Library 128
Internet 130
Cookies Class 130
DBF-> HTML 130
FRX->HTML 132
Hyperlink Object 132
Hyperlink Button 134
Hyperlink Image 135
Hyperlink Label 135
SCX->HTML 136
URL Combo 136
URL Open Dialog Box 137
Web Browser Control 138
Menus 139
Navigation Shortcut Menu 139
Shortcut Menu Class 140
Miscellaneous buttons 142
Cancel Button 143
Help Button 143
Launch Button 143
xi
OK Button 143
Run Form Button 144
Run Report Button (Preview Report) 144
Send Mail Buttons 145
Miscellaneous forms 146
About Dialog Box 146
Item Locator 147
Keywords Dialog Box 149
Messagebox Handler 150
Password Dialog Box 151
Splash Screen 152
Movers 153
Field Mover 153
Mover 154
Sort Mover 156
Super Mover 157
Table Mover 157
Multimedia 158
Sound Player 158
Video Player 159
Reporting 160
Output Object 160
Output Control 163
Output Dialog Box 163
Text Preview 164
System utilities 165
Application Registry 165
INI Access 167
ODBC Registry 168
Registry Access 169
Shell Execute 171
Text formatting 171
Font Combobox 172
Fontsize Combobox 172
Format Toolbar 172
RTF Controls 173
User interface 173
Get File and Directory 173
MouseOver Effects 174
Resize Object 174
Thermometer 175
Conclusion 176
xii
Chapter 5: OOP Standard Tools 177
The Class Browser 177
Basic functionality 177
Opening libraries 178
Renaming items 179
Redefining classes 180
The class icon 182
Creating source code 183
Different ways to start the Class Browser 184
Customizing and extending the Class Browser 185
What's the deal with the VCX file? 199
The VCX structure 199
How Visual FoxPro stores visual classes 202
Compiling a class library 204
Cleaning up class libraries 205
How to protect your source code in a VCX 205
The Component Gallery 205
Basic functionality 207
Working with existing catalogs 211
Creating catalogs 211
Conclusion 236
Chapter 6: Good Habits 237
Coding standards 237
Object naming conventions 237
Class naming conventions 239
Variable naming conventions 240
Field naming conventions 241
Method naming conventions 241
Other conventions 243
Design standards 244
Class hierarchies 244
Documenting 245
Comment lines in source code 245
Description fields 247
Formal documentation 249
Chapter 7: Evaluating Risks and Benefits 251
Benefits 251
Faster development 251
Higher quality 252
Easier maintenance 252
Reduced cost 253
Increased scalability 253
xiii
Better information structures and complexity management 254
Increased adaptability 254
Better mapping to the problem domain 254
Concerns 255
Need for standards 255
Speed of execution 255
Availability of qualified personnel 256
Costs of conversion 256
Support for large-scale modularity 256
Learning curve 257
SECTION 2—Advanced Object-Oriented Programming 259
Chapter 8: The Bigger Picture 261
This isn't all about subclassing buttons, is it? 261
Creating behavioral objects 261
How everything comes together 264
Creating reusable objects 266
Reuse—a fairytale? 266
Application-internal reuse 267
Cross-application reuse 268
Reusing components that don't match your architecture 269
Conclusion 269
Chapter 9: Three-Tiered Development 271
A brief introduction 271
The model-view-controller approach 272
Better than sliced bread: sliced applications 272
Three-tiered internally 272
Handling the data 273
Creating the interface 274
Invoking the business logic 275
Compiling one EXE 276
Displaying and manipulating data 276
Class and inheritance hierarchies 277
Exposed tiers 278
Conclusion 279
Chapter 10: Patterns 281
An overview 281
What exactly is an "object-oriented design pattern"? 282
Our first pattern 282
xiv
One common terminology 282
Some examples 283
Adapter pattern 283
Observer pattern 286
Strategy pattern 288
Template method pattern 291
Conclusion 293
Chapter 11: Object Metrics 295
The importance of metrics 295
Automated measuring 295
Metrics based on code complexity 297
Number of lines of code 297
Metrics based on class hierarchies 301
Number of classes 301
Number of custom properties 306
Number of methods 307
Number of "Is-A" relations 309
Metrics based on objects 310
Number of instances 311
Number of times a class is reused 311
Number of dependencies 311
Conclusion 311
SECTION 3—Object Modeling 313
Chapter 12: The Unified Modeling Language 315
Introducing the Unified Modeling Language 315
Some modeling history 315
The ideas behind UML 316
UML tools 316
UML notation 317
Packages and package diagrams 317
Use Case diagrams 320
Class diagrams 322
Interaction diagrams 336
Component diagrams 341
Deployment diagrams 343
Stereotypes 344
Conclusion 345
xv
Chapter 13: Collecting Requirements 347
What we try to achieve 347
Collecting the requirements 347
Research for shrink-wrapped applications 347
Interviewing customers 348
Lay a good foundation 350
Hitting a moving target 350
Managing requirements 351
Requirement lists 353
Rational RequisitePro 356
Conclusion 360
Chapter 14: Analyzing the Problem 361
A Use Case-driven approach 361
What exactly is a use case? 361
Transition of a requirement to a use case 363
Use case tools 375
Microsoft Word 375
Rational Rose 375
Chapter 15: Object Modeling 377
Object modeling—an overview 377
A picture is worth more than a thousand words 377
Object modeling tools 378
From use cases to objects 379
Identifying interface objects 379
Identifying the data services 384
Identifying the middle tier 387
Identifying methods and properties 391
Identifying common scenarios 392
Putting things together 394
You don't want three-tiered models? 394
Implementation details 394
Keeping things language independent 395
How Rose handles items 395
Reusing design 396
Chapter 16: Implementation 397
Creating Visual FoxPro code 397
The Code Generation Wizard 397
Importing your first model 399
Selecting model files more efficiently 403
Step 2's secret options 403
xvi
Resolving conflicts 406
Setting Visual FoxPro options in the model 408
Updating existing libraries 410
Filling in the blanks 410
Chapter 17: Documenting Existing Projects 411
Going back to the modeling tool 411
The Reverse Engineering Wizard 411
Creating diagrams 414
Updating existing models 415
Creating new models 415
Beyond basic modeling 416
A glimpse of the modeling future… 416
xvii
Acknowledgements
Over a year ago, I started to discuss with Whil Hentzen the possibility of writing a book
exclusively about object-oriented programming in Visual FoxPro. I knew Whil was planning a
series of books about Visual FoxPro 6.0, and to me, the series just wouldn't have been complete
without something on object-oriented development. After all, Visual FoxPro has an excellent
object model, and it will be a shame if people don't start using it more than they do now.
Luckily, Whil saw it the same way and we decided to do this book. So the first "thank you"
goes to Whil Hentzen for letting me do this in the first place.
When I started work on this book, I knew it was going to be a large task. Boy, was I
wrong! This task wasn't large—it was enormously huge! When you write about advanced
object-oriented programming, there is a large amount of research involved. Also, there is a lot
of text and few pictures (unless one draws pictures, but I'm a lousy artist…), so the pages filled
up very slowly. Also, the task of writing a book in a foreign language turned out to be not so
simple. But again, I was lucky: I had Mac Rubel as a technical editor, who gave lots of helpful
input and was of great assistance every time I stumbled over hurdles in the English language.
Thanks, Mac, I couldn't have done it without you! The same is also true for Jeana Randell, our
copy editor, so also "thanks" to you, Jeana!
I'm not a professional author. My job is to design and write object-oriented applications,
mainly as a consultant. I do a good amount of writing, and I also like to speak at conferences
and other events, but all of this is more personal pleasure than business. For this reason, writing
this book cut deep into my spare time, which wasn't only at my expense, but also at the expense
of my best friend and fiancée Martina. I want to use this opportunity to thank her for sticking
with me during this time, and even helping me out as much as possible. Thanks, Martina, and I
promise to have more spare time from now on!
The initial outline of this book was slightly different from what you hold in your hands
now. We decided to make some changes as we went along, mainly due to the fact that I
discovered things that weren't obvious before. The chapter that deals with the Fox Foundation
Classes is a good example. When I came up with an outline, I knew that there would be new
foundation classes, but nobody (not even Microsoft, I think) really knew what they would
include. Once I saw them, I realized how powerful and important this set of classes was, so I
decided to write much more about them than initially intended. However, this also required a
lot of input from Microsoft. Thanks to the entire Microsoft Visual FoxPro team for their help!
Obviously, it took me a while to even get to a point where I could write a book, and I didn't
get there all by myself. I want to thank my parents for supporting a crazy teenager who had
some wild ideas about writing software for customers all around the world who would be
serviced over the Internet. To many of us, this is part of our daily lives now, but then it was
science fiction, especially to the bankers. Without having my parents (and the rest of the
family) supporting me, I would have given up a long time ago. Thanks for this support!
Once I started my own business, I realized how hard it was to get a foot on the floor
without stumbling too much. At this time, people such as Jürgen "wOOdy" Wondzinski,
Steven Black, Andrew Ross MacNeill, the German FoxPro Users Group and basically the
entire FoxGang were extremely helpful. Thanks to all of you!
—Markus Egger