|
|
|
Introduction: You have probably heard this term tossed around quite a bit. It sounds pretty mysterious and has probably left you with many questions. Well let me tell you a little secret: you have already been doing object oriented programming since you started this book! Objects are basically templates that you manipulate. Every control you put on a form (and, incidently, the form itself) is an object. When you change the place a text box on a form and change one of its properties, what you have actually done is created an instance of the textbox object and changed one of its members. It is important to realize
that you deal with objects every single day. The car you
drive is an object. It has properties such as size, shape,
color,etc. It also has methods such as accelerate, brake,
etc. You don't have to know how a car works, you just have
to know how to call its methods. That is how an object works
in programming. Perhaps this example is
showing you why object oriented programming is so cool. You
can create a text box, manipulate its properties, and
utilize its events without having the slightest idea how a
text box is made. This is often referred to as the "Black
Box" approach. You can utilize the black box without knowing
how it works inside. You just have to know what data to feed
it. Before we move forward into
showing you how to create your own objects, lets discuss a
little object oriented terminology: OOP: This means object
oriented programming. Instance: This is basically
the current incarnation of your objects template. Think of
the general object "car" and you own care as a specific
instance of the "car" object. Class: This is a special
kind of code module that is used for you to make your own
objects. Encapsulation: This is
probably the single most important part of object oriented
programming. It basically refers to the fact that an object
stores both data and the methods to manipulate that data in
the same place. Inheritance: This refers to
the ability to make classes from other classes. This is not
available in Visual Basic. Polymorphism: This refers to
the ability to define a new object based on an existing
object and then simply alter the new object. This is not
available in Visual Basic. New: This keyword denotes a
new instance of an object. For example: Dim myclass as new originalclass Classes: Lets talk about classes,
since that is how you do object oriented programming. A
class is a special kind of code module. A class will have
certain items. Any subroutines or functions that you add
will be your class(objects) methods. Any variables you add
will be your class (objects) properties. When you first add a class
to your project it will have a declarations section and a
class section. Under the class section two events are there.
One begin intialize and the other terminate. The initialize
event is where you place any code that you wish to execute
as soon as an instance of the class is created. The
terminate event is where you place code when you wish to
execute when the class is destroyed. This is a good place to
put any "clean up" code. Under the general declarations
section you will declare any variables (properties) of your
class (object) that you might need. All variables should be
declared as private. One of the reasons you use a class is
to protect data. The variables can ONLY be accessed via
methods(subroutines and functions). Probably the most common
example of using a class is the deck of cards example. I
will utilize that example here. Lets say you wanted to
create a class that any programmer could use to make a card
game program. You would need to be able to create the
different cards in the deck and to shuffle the deck. So lets
create a class module named DeckofCards. Then we will
declare some variables in the declarations section of our
class: Private Deck (0 to 51) as integer 'this way we have a 52 card deck Private ThisCard as string 'this is to identify a particular card Private Position as integer
'this will identify where in the deck you are at Now when we initialize our
class we will need to create a deck of cards ranging from 0
to 51 (remember that arrays always start at 0). So lets
place this code in our classes initialize event: Private Sub Class_Initialize
() Dim I as integer 'used as a loop counter For I = 0 to 51 Deck(I) = I Next I End sub Now we can create a deck of
cards. However, a deck needs to do some things. For these
actions, we will add methods to our class. Lets start with
allowing our deck to be shuffled: Public Sub shuffle() 'While all variables
(properties) of our class (object) are private
(encapsulated) our 'subroutines and functions (methods) must
be public or else they cannot be accessed by 'anything
outside the class. In other words, our class would do
nothing! Dim x as integer Dim I as integer Dim itemp as integer Dim iPlace as
integer For I = 0 to 5199 '10 times through the deck IPlace = I mod 52 X = int(52 * Rnd) Itemp = deck(iPlace) Deck(x) = iTemp Next I End sub Now you could also make this
sub a function and pass to it an integer which would be the
number of times you want to shuffle. Now we can shuffle the deck,
but its is still a useless class if we cannot get the
current card from it. Public Property Get
CurrentCard() as string ThisCard = CalculateCard(Deck(position)) Current card = thiscard End property Now that we can get a card
we may want to deal these cards: Public function DealCards() as string If position > 51 then imessage = msgbox ("That's the end of the deck",vbokonly,"End of Deck") Thiscard = calculatecard(deck(position)) Dealcards = thiscard Position = position +
1 End function Of course, we will want to add in one more thing. We want to convert the numbers 0 to 51 to particular cards (Such as king of hearts): Private function calculatecard(x as integer) as string Dim Suit as integer Dim Value as integer Suit = x/13 Select Case Suit Case 0 Thiscard = "Spades" Case 1 Thiscard = "Hearts" Case 2 Thiscard = "Clubs" Case 3 Thiscard = "Diamonds" End Select Value = x Mod 13 Select Case value Case 0 Thiscard = " The Ace of " & thiscard" Case 1 to 9 Thiscard = str$(Value +1) & " of " & thiscard Case 10 Thiscard = " The Jack of " & thiscard Case 11 Thiscard = " The Queen of " & thiscard Case 12 Thiscard = " The King of " & thiscard End select Calculatecard =
thiscard End function Now you have a fully functioning deck of cards class! You can create an instance of it at any point in your code with a line such as: Dim mydeck as new deckofcards This object variable "deckofcards" is called an instance of the class "mydeck". Then you can access any of
its methods (i.e. functions and subroutines) in the same way
you access the properties and events of any other
control/object: Mydeck.shuffle Mydeck.dealcards Notice that this is exactly
the same as how you address the property of a control. That
is because the property of a control is actually a method of
that control class! While this particular class
did not use all the methods a class might use here is a list
of those methods and what they are used for: Let The property let would be used to change a property (variable) to a standard value (integer,string,etc). In our deckofcards example this was unneccessary since you would not want to change a cards value from outside the class. Get The get procedure is used when you want to get the value of a property (variable). For example if you wish to get the value of a card in our deckofcards example you can call the class and its get method: get current card: Mydeck.getcurrentcard* Set The property set is very
much like the property let, but is used with object
variables. *note the getcurrentcard
method calls several internal methods within the class,
however a programmer need not even know how those work in
order to utilize the deckofcards class. He or she only needs
to know what method of the class to call to get a
card Collections: Collections are basically object oriented arrays. Visual Basic has several built in collections. The forms collection contains a reference to all the form in a project and the controls collection contains a reference to all the controls on a given form. For example, if you wanted to disable all the text boxes on a given form you could use the controls collection like this: Private sub disable_text() Dim I as integer 'used as a counter Dim c as control 'use c as the generic control object For each c in
controls If typeof c is textbox then c.enabled = false end if next c You can also create your own
collections simply dimension a collection variable then add
stuff to it: Dim myCollection as new collection Now add stuff to your collection using the add method Mycollection.add somevariable Now you may ask why use a
collection instead of an array. Well that is a good
question, the answer is that a collection is far more
flexible. In an array all the elements must be of a single
data type. You can have an array of integer, and array of
strings, an array of doubles, but you cannot have a single
array that contains more than one type of variable. A
collection can contain diverse types of variables! Let's
look at another example. Dim empcollection as new collection Let sName = "Employee Name" Let iNum = 3848457 Let sPhone =
"1-555-555-5555 Empcollection.add sName Empcollection.add iNum Empcollection.add sPhone This added flexibility makes
the collection a far better alternative to the array. In the
real world, it is often necessary to use several different
data types. In my personal programming, I now only use
arrays with controls, in code I use only
collections. Destroy Your
Objects: It is a good idea to destroy
all objects before you exit a program. If you do not then
they are still floating around in memory. For example if you
used: Dim myclass as new
deckofcards Then you must
have Set myclass =
nothing The same is true with
collections. The nothing Keyword will cause the new instance
of the object to be destroyed.
|
|
|