Search Tools Links Login

GBIC: Objects


Visual Basic 6, or VB Classic

The following is reprinted for archival purposes from Gary Beene's Information Center, with permission from Mr. Beene himself.


'Objects are a combination of code and data which you can treat as a single item.

'The controls VB allows you to draw on a form are objects, are as the forms themselves.

'Properties, Methods, and Events
'All objects share a common way of accessing the data and code that we've said make up an object.

'The data inside an object is known as the properties of the object. In familiar terms, it corresponds
'to the values of variables which are stored inside the object. For example, all objects have names.
'The name is one of the properties of the object and the way that you access a property in code is as follows:

MyString = ObjectName.Name

'All properties of objects work exactly the same way, using what is called the "dot" notation. Here are some more examples:

h = Form1.height
w = listbox1.fontname
r = textbox.backcolor
g = timer1.interval

'In each of these examples, a variable on the left is assigned the value of the property on the right.
'The format for identifying the property name is also done the same in every case - "objectname.propertyname".

'Typically, whoever creates the object "exposes" properties which you can access. In the VB IDE
'environment you can click on a control and the properties box (press F4) will display all of the property
'names along with possible values for those properties. Object properties are just like variable in that they
'can have a type. They can be integer, string, dates, or any other VB data type. So remember when
'assigning property values to variables that the VB rules for type conversion must be taken into consideration.

'When in the IDE code windows, VB also provides a big help in remembering the properties of an object.
'Once you've typed in the object name and the ".", VB will display a dropdown listbox which give all of the
'available properties. This features is called Intellisense.

'Two other nuances about properties will be of interest. First of all, not all properties of an object
'are "exposed" for use in your VB program. When we get around to creating our own objects in
'code, you'll find that you can choose whether to expose a property for use in code, or whether
'to keep it hidden within the object (so as not to allow its value to get changed).

'COM & Exposed Objects
'One of the key features about objects is that they can be made available outside of an application for
'use by other programmers. Making an object available for use is known as "exposing" the object.
'Many of the major applications such as Word or Excel expose objects for your use. Microsoft has
'established a standard known as COM (Common Object Model) which guides programmers through
'the techniques of exposing objects from within their applications.

'Exposed objects cannot, however, be distributed with your application. If you plan to create an
'application which uses exposed objects from a second application, then your user must have
'that second application installed on his PC. This is not a trivial problem. Unless you have some
'control over the user's environment you'll find it best to distribute stand-alone applications - ones
'which do not assume that any particular software is resident on your user's PCs.

'All controls, whether in the form of ActiveX Controls (.OCX files) or in the form of exposed objects,
'must be registered with Windows (as part of the Registry) before your VB program can use the
'object. In the VB IDE you can call up the Components Dialog Box which will list all of the objects
'(ActiveX files or exposed objects) thtat are available for your use. If you select one of the objects
'for use, the object will appear on your VB IDE toolbox and can then be used just like any of the
'intrinsic controls that you have already used.

'There is a utility called regsvr32.exe that comes with Windows which you will find very useful.
'Regsvr32 allows you to manually register an OCX in the registry. The need to do this manually
'can arise for a variety of reasons, but mostly because an installation program failed to automatically
'make the registration. The code to register an OCX file is:

regsvr32 filename.ocx

'You can do this from a DOS command line or through the Start/Run menu option.

'Classes
'We're just about to get into the How of creating objects, but one more topic needs discussion
'first. Before an object can be created, an object template must first be created. Once that
'template is created a copy can be created. It's the copy with which you will work in your VB programs.

'In VB terminology, the template is called a class and the copies of the template are called
'instances. In the registration process that we just discussed, it is the template, or class, of an
'object that gets registered with Windows.

'Object Browser
'VB provides a tool with which you can browse all registered objects, including the properties
'and methods which they expose. The Object Browser window can be displayed by pressing
'F2 within the VB IDE. One advantage of the Object Browser over the properites window is
'that the browser can show both properties and methods of the registered classes. Additionally,
'the browser shows all registered objects, including the objects exposed by applications that
'you have installed on your PC. It's the poor man's documentation of all usable classes on your PC!

'Creating Objects - References, Class Modules, & Collections
'You can create objects at both design time and run time. Controls, forms, and class modules
'are examples of objects which you can create at design time (within the VB IDE). You can also
'create controls and form objects at run time, as well as creating variable objects and collections
'at run time. We'll cover these topics next in the tutorial.

'References
'It is possible use the DIM function to create a variable whose type is that of any registered
'class. To do so, use this code:

Dim X As ListBox

'Variables which have been defined as an object type are useful because they can be made
'to refer to an existing object. For example, this code, when used with the line of code just given,
'associates X with the ListBox1 control.

Set X = ListBox1

'Notice the use of the Set statement? To establish a variable reference to an object, you
'cannot simply use the equal sign. You must use the SET statement.

'There are several reasons why you should be interested in creating a second reference to a control.

'First of all, once the variable is set to reference the control, all of the control's properties
'and methods and be accessed as follows:

X.name
Is the same As
ListBox1.name

'If nothing else, using variables rather than the full name of the control can save you some
'typing as you write your code.

About this post

Posted: 2021-02-11
By: ArchiveBot
Viewed: 149 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.