Objects in Visual Basic
An object is a combination of code and data that can be treated as a unit. An object can be a piece of an application, like a control or a form. An entire application can also be an object. Objects are things that you can program with, things that make programming easier. They contain a set of methods and properties that allow you to make the object do certain things without actually having to look at the objects code. For example, you have probably used the following statement many times:
Text1.Text = ""
Remember that? Well, what you are doing is setting the property Text, of the text box object Text1 to equal nothing. Objects are really easy, In fact, all the controls you see on your VB toolbox are all objects. For a better look at objects, either press F2 or click View, Object Browser. This nifty little window allows you to see all the properties and methods of the objects currently loaded. Although Visual Basic is not a completely object orientated language it does allow us to use objects in our code.
You can declare an object using either the Dim, Private or Public keyword. As with other variables, the Private and Public keywords can only be used in the General Declarations procedure. You will probably know that declaring something as Public allows anyone to access the variable, and declaring it as Private only lets the current module of code access the variable.
So, what can you do? Well, simply avoid using the New keyword when declaring the object. Every time you want to use it, create a new instance of it and then destroy it When dealing with objects you use the Set keyword to perform some operations. When we want to destroy an object, we set it to nothing, quite literally:
Objects let you declare variables and procedures once and then reuse them whenever needed. For example, if you want to add a spelling checker to an application you could define all the variables and support functions to provide spell-checking functionality. If you create your spelling checker as a class, you can then reuse it in other applications by adding a reference to the compiled assembly. Better yet, you may be able to save yourself some work by using a spelling checker class that someone else has already developed.
Each object in Visual Basic is defined by a class. A class describes the variables, properties, procedures, and events of an object. Objects are instances of classes; you can create as many objects you need once you have defined a class.
To understand the relationship between an object and its class, think of cookie cutters and cookies. The cookie cutter is the class. It defines the characteristics of each cookie, for example size and shape. The class is used to create objects. The objects are the cookies.
Two examples in Visual Basic might help illustrate the relationship between classes and objects.
• The controls on the Toolbox in Visual Basic represent classes. When you drag a control from the Toolbox onto a form, you are creating an object — an instance of a class.
• The form you work with at design time is a class. At run time, Visual Basic creates an instance of the form's class — that is, an object.
Objects newly created from a class are often identical to each other. Once they exist as individual objects, however, their variables and properties can be changed independently of the other instances.
|All times are GMT +5.5. The time now is 06:11.|