
VB 4, Classes and OO stuff (LONG)
*** VB4, Classes and Stuff ***
Target audience: VB experts, intermediate developers
I am nearly finished with my first 'real' VB4 project using classes, here
are some thoughts. I dont purport to be the definitive expert on VB
classes or OO, but maybe you will find this of interest, if you havent
explored Classes:
* Project *
The project is not the biggest thing I have ever worked on but, it is
large enough to test some of the new stuff on VB4. Essentially it is an
insurance program to calculate the rate for boats and yachts. There are
lots of business rules involved (over 26 ft OR over $50k is a yacht; no
coverage for over 400 HP etc).
Target platform: VB4/16 for Win31/W95
* Classes and Objects *
If you haven't toyed with these, I urge you to do so now. They are Mondo
Cool. They more or less replace UDTs, dynasets and arrays. At the same
time, don't be scared of them: they are little more than smart UDT's with
procedures embedded in them.
For instance, when I set the .VLen property of the CVsl (Vessel) object,
it evaluates it and if over 26 ft, sets the class code to "Y", othewise
"B" (the boat-yacht thing).
This allows you to encapsulate ALL the BRs, DVs and DT (Biz rules, data
validation and data transformations) in one place: the class module. For
instance, the CVsl.IsValid property, when polled (PropertyGet), goes thru
all the class members (properties) and tests the required ones for in
range values. In so doing, it stores a string with all the errors ("HP,
Beam, Weight are required items"), that can be fetched later for display
to the user.
One thing the app has to do is perform a speed check. Given the weight,
waterline, HP etc of the vessel what does this formula say the Max speed
is? When the CVsl.MaxSpeed property is polled (PeopertyGet, again) the
formula is calculated and the Max speed returned.
There are 2 cool things here: .MaxSpeed is ReadOnly - there is no
PropertyLet, since the class creates the data. 2) The speed formula is
not even in the Vessel class - it is in a Speed Class, since it is used
elsewhere. Very cool and allows for MAXIMUM reusability of code.
Is it OO? Well the above describes Data hiding, abstraction and
polymorphism. 3 out of 4 aint bad. It misses on inheritance, but you can
get the benefits of inheritance by reversing it: rather than child objects
inheriting from the parent, just make the child a part of the parent.
But really, who cares? Depending on how pure you want to get in your
definition most anything can be disqualified. Delphi users poo-poo VB,
C++ users dump on delphi (it only supports single inheritance) and even
the Smalltalk people can make an argument that C++ is not "truly" OO.
I no longer care, and it does not matter in the long run: if it is
maintainable, encapsulated and polymorphic (the speed class never knows
when it is invoked from code or from the Vessel class; the IsValid method
works diffrently on the Vessel, Quote and Engine classes...), arent there
some benefits to be gained? Yea, my Vb4 car only goes 75 mph, and that
Delphi one goes 85, BUT my old VB3 car only went 60, so I am better off.
* VB Class Tips and Traps *
Beware your data types. When you create a new Property from the menu, the
type defaults to Variant. Take you time and re type them. Eg:
Public PropertyLet VLen(vNewValue)
End Property
Public PropertyGet VLen()
End Property
when you add in the code, reset the property data type:
Public PropertyLet VLen(n as Integer)
miLen = n
End Property
Public PropertyGet VLen() As Integer
VLen = miLen
End Property
1) If you fix PropGet and forget PropLet it wont run, but it will point to
one of the Prop procs and tell you the data types do not match. this is
good. But SOME SLIP BY and the code runs...UNTIL YOU MAKE EXE, then the
damn thing just says one of the property types are mismatched and does not
tell you which one!!!!!! Major PITA! take your time!!!.
I have used all dialects of MS/IBM basic since BASCOM