Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Program Objects

A class is a packaged object, with behaviors and properties that describe members of the class.

Objects that you create with your application are objects that are members of a particular class. For example, an option button class defines properties, events, and methods that all members of the option button class support. In other words, even though your application may contain five option buttons, and even though all five of those option buttons differ in one or more of their properties (such as Caption), they are all members of the same class. A command button can never be a member of the option button class because a command button's properties, events, and methods differ from an option button's.

You can test for membership within any given class. The class forms a hierarchy and all members of the class take on the class properties, events, and methods. One of the reasons for a class test is that you can pass to procedures not only variables, but also controls. The following procedure receives a command button as its only argument:

Public Sub GetIt(cmdClick As CommandButton)

Some procedures can be multipurpose. In other words, a procedure might change the BackColor property of whatever object you pass to that procedure. Use the As Object argument declaration as follows to make the procedure multipurpose:

Public Sub ChangeColor(objOnForm As Object)

You haven't seen the Object keyword until now, but you can declare not only arguments as Object datatypes, but variables as well, like this:

Dim objAnything As Object

The objAnything variable can now represent an object.

Your application's code can create any object needed at runtime. In other words, you could declare an array of five option buttons like this:

Dim ctlOpButtons(1 To 5) As New OptionButton

The New keyword tells Visual Basic to create five new option buttons. If you want to base a new object on an existing object, you only need to change the properties that differ in the new object from the old one. The following statement declares a new form based on an existing form named frmAcctsPay:

Dim frmNewForm As New frmAcctsPay

Notice that if you place an existing control name after the New statement, Visual Basic declares a new object based on an existing one. If you use a control's class name (such as CommandButton, Form, OptionButton, or Label), Visual Basic declares a new control with all default property values (except for the Name property, which you set with the Dim statement as you declare the control). You then can specify the property values that you want for your new object.

Use the If TypeOf...Is programming block to test for an object's datatype. The following If generates True if the object stored in objAnything is a text box:

If TypeOf objAnything Is TextBox Then

In addition to being a keyword command, Visual Basic supports the TypeOf() function that returns the object type of its argument.

Knowing about an object's class lets Visual Basic accept the following code that contains a With keyword block:

With lblTitle
   .Caption = "Accounts Payable"
   .Alignment = vbRightJustify
   .Font.Size = 15
   .Font.Bold = True
   .Left = 25
   .Right = 0
   .Width = 1000
End With

If you must set more than two or three properties in code, use With, which tells Visual Basic that all objects without an object qualifier are label objects. Without the With keyword, you would have to type the object's name all through the assignments, like this:

lblTitle.Caption = "Accounts Payable"
lblTitle.Alignment = vbRightJustify
lblTitle.Font.Size = 15
lblTitle.Font.Bold = True
lblTitle.Left = 25
lblTitle.Right = 0
lblTitle.Width = 1000

Share ThisShare This

Informit Network