[ Pobierz całość w formacie PDF ]
.We call these variables instances of theobject.Building VB ObjectsLet s take a very simple example.Suppose that we want to design an objectfor measuring distance.Now, our first thought might have been to simplywrite a little subroutine to execute the measurement, and then perform themeasurement each time by calling this subroutine.But in VB, we can write our code as a series of objects.So rather than writingsubroutines," We create a TapeMeasure class" We create instances of that class, each with different sizes" We ask each instance to draw itself.In VB, objects are represented as class modules Each VB class is anobject which can have as many instances as you like.When you write a VBprogram, the entire program is one or more classes.The main class representsthe running program itself, and it must have the same name as the programfile.In our example, the program is called Measurer.cls and the main class iscalled Measure.frm.Copyright © 2001, by James W.Cooper 59Classes in VB contain data and functions, which are called methods.Both the data and the methods can have either a public or a privatemodifier, which determines whether program code outside the class canaccess them.Usually we make all data values private and write publicmethods to store data and retrieve it from the class.This keeps programs fromchanging these internal data value accidentally by referring to them directly.If we want users of the class to be able to use a method, we, of course,must make it public.If on the other hand, we have functions or subs that areonly used inside the class, we would make them private.A VB program canbe made up of any number of.cls and.frm files.Creating Instances of ObjectsWe use the new operator in VB to create an instance of a class.For exampleto create an instance of the TapeMeasure class, we could write:Dim tp as TapeMeasure variable of type TapeMeasure create instance of TapeMeasureset tp = new TapeMeasureRemember, while we can create new variables of the primitive types (such asInteger, Single, etc.) we must use the new operator to create instances ofobjects.The reason for this distinction is that objects take up some block ofmemory.In order to reserve that memory, we have to create an instance of theobject, using the new operator.A VB Measurement ProgramIn the example below, we see a complete TapeMeasure class, including itsmeasure routine.'Tape measure classPrivate width As Single, factor As SinglePublic Sub setUnits(units As String)'allows units to be cm or feetSelect Case LCase$(units)Case "c": 'centimetersfactor = 1Case "f": 'feetfactor = 2.54Case ElseCopyright © 2001, by James W.Cooper 60factor = 1End SelectEnd SubPublic Function Measure() As Singlewidth = Rnd * 100#Measure = width / factorEnd FunctionPublic Function lastMeasure() As SinglelastMeasure = width / factorEnd FunctionThe calling program is the Measurer form, which is merely the following:Dim tp As New TapeMeasurePrivate Sub btMeasure_Click()txMeasure.Text = Str$(tp.Measure)End SubPrivate Sub opCM_Click()tp.setUnits "c"txMeasure.Text = Str$(tp.lastMeasure)End SubPrivate Sub opFt_Click()tp.setUnits "f"txMeasure.Text = Str$(tp.lastMeasure)End SubMethods inside ObjectsAs we noted above, functions inside a class are referred to as methods.Thesefunctions can be public, meaning that you can access them from outside theclass, or private, meaning that they can only be accessed from inside theclass.VariablesIn object oriented programming, you usually make all of the variables in aclass private as we did above with width and factor.Then you set the valuesof these variables either as part of the constructor or using additional set andget functions.This protects these variables from accidental access fromoutside the class and allows you to add data integrity checks in the setfunctions to make sure that the data are valid.Copyright © 2001, by James W.Cooper 61We could, of course, have made the TapeMeasure s factor variable publicand set it directly.tp.factor = 2.54;but this gives the class no protection from erroneous data such as:tp.factor = -50;So instead, we use accessor functions such as setUnits to make sure thatthe data values we send the class are valid:tp.setUnits cand then within the class we write this accessor function with some errorchecking:Likewise, since the TapeMeasure class saves the last measurement it makes,you can always read it back by calling a lastMeasure method.Passing Arguments by Reference and by ValueBy default, all variables are passed into methods by reference.In other wordsthe original data can be accessed and change within any class method.Public Sub setTemp(t As Single)t = 5 changes t in the calling programEnd SubTo avoid this happening by accident, you should make a habit of prefixingyour arguments with ByVal, which copies the value into the subroutine
[ Pobierz całość w formacie PDF ]