< Previous lesson | Table of Content | Content of this Chapter | Next lesson >

How to use Object in Visual Basic?

All controls in the Visual basic Toolbox are classes. When you do drag and drop those controls into a form, you are actually taking a particular object from that class. Form itself is also a class. Form has several methods such as Load (to load new form in memory but does not show it), Show (to display the form), Unload (to remove the form from memory), and Hide (to hides the form, but does not remove from memory), Move (to move the form), and so on.

To use an object variable is similar to use a conventional variable, but with one additional step assigning an object to the variable:
First you declare it:
Dim variable As class


Then you assign an object to an object variable with the Set statement:
Set variable = [ New ] object


You declare an object variable in the same way you declare other variables, with Dim , ReDim , Static , Private , or Public . The only differences are the optional New keyword and the class argument. The New keyword is used to create object, an instances of forms, controls, classes defined in class modules, or collections. The syntax is:


{Dim | ReDim | Static | Private | Public} variable As [New] class

To set or get the Property of an object, it is similar to assigning to or to obtain value from a variable. To call the Method of the object is similar to call function or procedure. To set the value of a property, use the following syntax:
object.property = expression

Example:
Text1.Top = 200 ' Sets the Top property to 200 twips.
Text1.Visible = True ' Displays the text box.
Text1.Text = "hello" ' Displays 'hello' in the text

To get the value of a property, you use the following syntax:
variable = object.property

Example:
myVar = Text1.Text

Calling an object method is using the following syntax:
[varName =] object.method [arg1, arg2, , argN]

For Example:

Me. Show To show the Form
Form1.Circle (1600, 1800), 1200, vbBlue Draw a blue circle with a 1200-twip radius
Picture = Clipboard.GetData (vbCFBitmap) To returns a picture from the Clipboard

< Previous lesson | Table of Content | Content of this Chapter | Next lesson >