Creating objects
The easiest way to create an object is to double-click the control in the Toolbox. However, for all of the available objects in Visual Basic and all available objects from other applications, to fully demonstrate their advantages, you can create objects at run time with Visual Basic programmable performance.
1. Create an object reference with an object variable
2. Create your own objects with the class module, "Start from scratch"
3. Create your own collection with the collection object
Details in other chapters will describe how to access the object. For example, the CreateObject and GetObject functions are discussed in chapter tenth, "Programming with parts."
Working with object variables
In addition to storing values, variables can refer to objects. As with variable assignment, objects can be assigned to variables for the same reason:
1. Variable names tend to be shorter and better than the values contained in variables (or, in this case, the values they refer to).
2. When running code, you should change the variable in order to refer to other objects.
3. Referencing a variable that contains an object is more efficient than repeatedly referencing the object itself.
Using an object variable is just like using a regular variable, but it's more of a step to assigning an object to a variable:
4. Declare the variable first:
Dim variable as Class
5. Then assign the object to the variable:
Set variable = object
Declaring an object variable
declares an object variable in the same way as declaring other variables, using Dim, ReDim, Static, Private, and public. The only difference is that the new keyword and class parameters are optional, and the new keyword and class parameters are described later in this chapter. The syntax is:
{Dim | ReDim | Static | Private | public} variable as [new] class
For example, you can declare an object variable that references the form in the application that is called Frmmain:
Dim formvar as New frmmain ' declares a type of frmmain< br> ' object variable. The
can also declare an object variable that references any form in the application:
Dim anyForm as Form ' general form variable.
Similarly, you can declare an object variable that can reference any text box in the application:
Dim anytext as TextBox ' can refer to any text box
' (but only a text box). The
can also declare an object variable that can reference any type of control:
Dim Anycontrol as control variable.
Note that you can declare a form variable that references a specified form in your application, but you cannot sound a control variable that references a particular control. You can declare a control variable that can refer to a particular type of control, such as a TextBox or list box, but not a particular control that refers to that type (such as Txtentry or List1). However, you can assign a particular control to that type of variable. For example, a form with a list box lstsample can be encoded like this:
Dim Objdemo as ListBox
Set Objdemo = lstsample
An
Assignment object variable
assigns an object to an object variable with the SET statement:
Set Variable = Object
You can use the SET statement whenever you want an object variable to refer to an object.
Sometimes you can use object variables, especially the available control variables, to shorten the code you want to type directly. For example, the original code is like this:
If frmaccountdisplay!txtaccountbalance.text < 0 Then
frmaccountdisplay! Txtaccountbalance.backcolor = 0
Frmaccountdisplay!txtaccountbalance.forecolor = 255
End If
If you use a control variable, Programs can be significantly shortened:
Dim Bal as TextBox
Set Bal = frmaccountdisplay!txtaccountbalance
If bal.text < 0 Then
Bal.backcolor = 0
Bal.forecolor = 255
End If
Specific object types and generic object types
A particular object variable must reference a particular type of object or class. A specific form variable can reference only one form in the application (although one can be referenced in many instances of the form). Similarly, a particular control variable can refer only to a specific type of control in the application, such as a TextBox or a list box. Take a look, open a new project, and place a text box in the form. Add the following code to the form:
Private Sub Form_Click ()
Dim Anytext as TextBox
Set Anytext = Text1
Anytext.text = "Hello"
End Sub
Run the application and click the form. The Text property of the textbox becomes "Hello".
Generic object variables can refer to one of a variety of specific object types. For example, a generic form variable can refer to any form in the application, and a generic control variable can reference any control on any form in the application. For example, open a new project and place several Frame, Label, and CommandButton controls in any order in the form. Add the following code to the form:
Private Sub Form_Click ()
Dim Anycontrol as Control
Set Anycontrol = Form1.controls (3)
Anycontrol.caption = "Hello"
End Sub
Run the program and click the form. The title of the third control that you just put into the form will change to "Hello".
There are four general objects in Visual Basic:
Generic object variables are useful when you do not know whether a particular type of object is referenced when the variable is run. For example, if you want to write code and make it operational in any form in your application, you must use a generic form variable.
Note Because there can be only one MDI form in an application, you do not have to use the generic MDIForm type. Whenever you need to declare a form variable that references an MDI form, you can use a specific MDIForm type (either MDIForm1, or whatever name you specify for the MDI form's Name property), rather than the generic MDIForm type. In fact, because Visual Basic can decide on properties and methods that reference a particular form type before running the application, you should always use a specific mdiform type.
It is useful to have more than one MDI form in a single application in a future version of Visual Basic, given the general MDIForm type only for completeness.
form as an object
Forms are most commonly used to construct the application interface, but are often the objects called by other modules in the application. Forms are closely related to class modules. The main difference between the two is that the form is a visual object, but the class module has no visual interface.
Adding Custom methods and properties
You can add custom methods and properties to a form and access them from other modules in your application. To create a new method for a form, add a procedure that is declared with public.
' Customize the Form1 method.
Public Sub Latejobscount ()
.
. ' <statements>
.
End Sub
You can invoke the Latejobscount procedure from another module using the following statement:
Form1.latejobscount
Creating a new property of a form is as simple as declaring a common variable in a form module:
Public IDNumber as Integer
The following two statements can be used to set and return Form1 IDNumber values from other modules:
Form1.idnumber = 3
Text1.Text = Form1.idnumber
You can also add custom properties to the form by using the property procedure.
Detailed information in the Nineth chapter, "Programming with objects" provides detailed material for the property process.
Note You do not have to load the form to call a variable or custom method of the form, and you can set the form's custom properties. Instead of loading the form into memory, you can run code about the form. Similarly, a form is not loaded when a control is referenced without referencing its properties or methods.
Use the NEW keyword
Creates a new object that is treated as the object defined by its class. You can use new to create collections, instances of forms, and classes defined in class modules.
The form that
uses the New keyword on a form
to create at design time is a class. You can create a new instance of this class with the new keyword. To see this work process, draw a command button and several other controls on the form. In the Properties window, set the form's Name property to Sample. Add the following code to the Click event procedure for the command button:
Dim x as New Sample
X.show
Run the application and click the command button several times. Move the front form to one side. Because a form is a class with a visual interface, you can see an additional copy. Each form has the same controls at the same location, and these locations are exactly where the form is at design time.
Note to make form variables and instances of loaded forms persistent, use Static or public variables instead of local variables. The
can also use the NEW keyword with the Set statement. Try the code in the Click event procedure for the command button:
Dim f as Form1
Set f = new Form1
f.caption = "Hello"
f.show
Use the NEW keyword and the SET statement to speed up Running speed, therefore, this method is worth recommending.
Use the NEW keyword for other objects
Use the NEW keyword to create collections and objects from classes defined in the class module. Try the following example to illustrate this work process.
This example shows how the NEW keyword creates an instance of a class. Open a new project and draw a CommandButton control on the Form1. Select the Add Class Module command from the Project menu to add a class module to the project. Set the class module's Name property to ShowMe.
The following code in FORM1 creates a new instance of the class ShowMe and calls the procedure contained in the class module.
Public Clsnew as ShowMe
Private Sub Command1_Click ()
Set clsnew = New ShowMe
Clsnew.showfrm
End Sub
The SHOWFRM procedure in the class module creates a new instance of the class Form1, displays the form, and then minimizes it.
Sub showfrm ()
Dim Frmnew as Form1
Set frmnew = New Form1
Frmnew.show
Frmnew.windowstate = 1
End Sub
To apply this example, run the application and click the command button several times. When you create each new instance of the ShowMe class, you see the minimized form icon appearing on the desktop.
For more information about creating objects with New, see Chapter Tenth, "Programming with parts."
Limitations of the NEW keyword
The following table describes what you cannot do with the new keyword.
Releasing a reference to an object
Each object uses both memory and system resources. It is good programming practice to release these resources in time when objects are no longer in use.
Unloads a form or control from memory with Unload.
Frees the resource occupied by object variables with nothing. Assigns nothing to an object variable with the Set statement.
For more information, see the "Unload events" and "Nothing" sections in the Visual Basic 6.0 Language Reference manual.
Passing objects to a procedure
You can pass an object to a procedure in Visual Basic. In the following code example, assume that there is a CommandButton control in the form:
Private Sub Command1_Click ()
' Invoke the Demo child procedure and pass the form to it.
Demo Form1
End Sub
Private Sub Demo (x as Form1)
' Center the form on the screen.
X.left = (screen.width-x.width)/2
End Sub
You can pass an object to a parameter by reference, and then set the parameter to a new object within the procedure. To see this work process, open the project and insert the second form. Place a picture box in each form. The property setting values that need to be changed are as shown in the following table:
The Form1_Click event procedure calls the Getpicture procedure in Form2 and passes an empty picture frame to it.
Private Sub Form_Click ()
Form2.getpicture Picture1
End Sub
The getpicture process in Form2 assigns the Picture property on the Form2 to the empty picture frame on the Form1.
Private OBJX as PictureBox
Public Sub getpicture (x as PictureBox)
' assigns the passed in picture frame to the object variable.
Set OBJX = x
' Assign the Picture property value to the Form1 frame.
Objx.picture = Picture2. Picture
End Sub
To apply this example, run your program and click Form1. In the Form1 picture box, you will see the icon that appears in the Form2.
Detailed information The above topics are intended to outline objects. For more information, see Chapter Nineth "Programming with Objects" and chapter tenth "Programming with parts".