Home

Introduction to Objects Characteristics

 

Classes and Objects

 

Introduction

The language of Microsoft Visual Basic uses the concept of class to identify or manage the parts of an application. Consider an object like a house. It has such characteristics as its type (single family, town house, condominium, etc), the number of bedrooms, the number of bathrooms, etc. These characteristics are used to describe a house to somebody who wants to buy it. To get such an object, you must first define the criteria that describe it. Here is an example:

House
[
      Address
      Type of House
      Number of Bedrooms
      Number of Bathrooms
      Has Indoor Garage
      The Living Room is Covered With Carpet
      The Kitchen Has an Island Stove
]

This information is used to describe a house. Based on this, the House is called a class. To actually describe a real house, you must provide information for each of the above characteristics. Here is an example:

House: Langston
[
      Address: 6802 Leighton Ave
      Type of House: Single Family
      Number of Bedrooms: 4
      Number of Bathrooms: 3
      Has Indoor Garage: Yes
      The Living Room is Covered With Carpet: Yes
      The Kitchen Has an Island Stove: No
]     

In this case, Langston is not a class anymore, it is a real house and is explicitly described. Therefore, Langston is called an object. Based on this, a class is a technique used to provide the criteria to define an object. An object is the result of a description based on a class.

Properties

In our example of a house, we used words to describe it. Examples are: Address, Type of House, Number of Bedrooms, Number of Bathrooms. In computer programming, the characteristics used to describe an object are referred to as its properties.

To display the characteristics of a Windows control, whether you are accessing it from Microsoft Excel or Visual Basic, you can right-click it and click Properties. This would open the Properties window and display the characteristics of the selected control:

The Properties window allows you view or change a characteristic of the control. The properties of an object can be changed when designing it or by writing code. You can also manipulate these characteristics both at design and at run times. This means that you can set some properties at design time and some others at run time.

The items in the Properties window display in two lists. In the beginning, you may be regularly lost when looking for a particular property. To get the list in alphabetic order, click the Alphabetic tab. To get the list by categories, click the Categorized tab.

When a control is selected, the Properties window displays only its characteristics. When various controls have been selected, the Properties window displays only the characteristics that are common to the selected controls.

 

Practical Learning Practical Learning: Viewing the Properties of a Control

  1. To view the properties of the button, on the worksheet, right-click the CommandButton you added and click Properties
  2. To see the properties of a Visual Basic object, on the Taskbar, click Microsoft Visual Basic.
    In the lower-left section of the screen, notice that the Properties window displays the characteristics of the form

Introduction to the Properties Window

 

Introduction

After you have added a control to a worksheet or a form, in order to perform any type of configuration on the control, we saw in the previous lesson that you must first select it. Besides the techniques we reviewed in the previous lesson, to select a control, if you know its name, you can click the arrow of the combo box in the top section of the Properties window and select it:

 

 

The Name of a Property

After adding a control to your application, you can manipulate its characteristics. If you are working in Microsoft Excel, to put a control into edit mode, in the Control Toolbox, click the Design Mode button Design Mode.

Each field in the Properties window has two sections: the property’s name and the property's value:
 

Properties Names and Values

The name of a property is represented in the left column. This is the official name of the property. Notice that the names of properties are in one word. Based on this, our House class would have been defined as follows:
House
[
      Address
      TypeOfHouse
      NumberOfBedrooms
      NumberOfBathrooms
      HasIndoorGarage
      LivingRoomCoveredWithCarpet
      KitchenHasIslandStove
]

You can use this same name to access the property in code. 

 

Accessing a Control's Property

To access a property of a control using code, type the name of the control, followed by a period, followed by the name of the property. Based on this, if you have a House object named Langston, to access its TypeOfHouse property, you would write:

Langston.TypeOfHouse

 

The Value of a Property

The box on the right side of each property name represents the value of the property that you can set for an object. There are various kinds of fields you will use to set the properties. To know what particular kind a field is, you can click its name. To set or change a property, you use the box on the right side of the property’s name: the property's value, also referred to as the field's value.

 

Changing the Value of a Control's Property

To programmatically change the value of a property, type the name of the control, followed by a period, followed by the name of the property, followed by =. Then, on the right side of equal, you must provide the value but this depends on the type of value.

The people who developed the controls and worksheets also assigned some primary values to their properties. This is the type of value that a property either is most likely to have or can use unless the user (that is, you) of a control decides to change it. The primary value given to a property is referred to as its default value. Most of the time, you can use that property. In some other assignments, the default value will not be suitable.

Types of Properties

 

Empty Fields

 

By default, these fields don't have a default value. Most of these properties are dependent on other settings of your program. To set the property on such a field, you can type in it or sometimes you will need to select from a list. 

 

Text Fields

There are fields that expect you to type a value. Most of these fields have a default value.

To change the value of the property, click the name of the property, type the desired value, and press Enter or Tab. While some properties, such as the Caption, would allow anything, some other fields expect a specific type of text, such as a numeric value.

To change the value of a text-based property, on the right side of the = sign, you can type it in double quotes. For example, suppose you have a House object named Langston. If you want to specify its address, you would write:

Langston.Address = "6802 Leighton Ave"
 

Numeric Fields

Some fields expect a numeric value. In this case, you can click the name of the field and type the desired value. If you type an invalid value, you would receive a message box notifying you of the error:

When this happens, click OK and type a valid value. If the value is supposed to be an integer, make sure you don't type it as a decimal number.

 

Expandable Fields

Some fields have a - or a + button. This indicates that the property has a set of sub-properties that actually belong to the same property and are defined together. To expand such a field, click its + button and a – button will appear.

To collapse the field, click the – button. Some of the properties are numeric based. With such a property, you can click its name and type the numeric value. Some other properties are created from a sub-list. If you expand such a field, it would display various options. With such a property, you should select from a list.

Boolean Fields

 
Some fields can have only a True or False value. To change their setting, you can either select from the combo box or double-click the property to toggle to the other value.

To specify the value of a Boolean property, on the right side of the = symbol, you can provide its value as True or False. Here is an example:

Langston.HasIndoorGarage= True

 

 

Intermediary Fields

Some fields would require a value or item that needs to be set through an intermediary action. Such fields display an ellipsis button . When you click the button, a dialog box would come up and you can set the value for the field.
 

List-Based Fields

To change the value of some of the fields, you would use their combo box to display the available values. After clicking the arrow, a list would display:

Property Selection

There are various types of list-based fields. Some of them display just two items. To change their value, you can just double-click the field. Some other fields have more than two values in the list. To change them, you can click their arrow and select from the list. You can also double-click a few times until the desired value is selected.

To specify the value of a list-based property, you must use one from a list. For example, suppose you had defined a list of types of house as tpeSingleFamily, tpeTownHouse, and tpeCondominium. To use one of these values for a House object named Langston, you would type:

Langston.TypeOfHouse = tpeSingleFamily

In most cases, each member of such a list also uses a natural number. An example would be:

TypeOfHouse Value
0 tpeSingleFamily
1 tpeTownHouse
2 tpeCondominium

Although we used 0, 1, and 2 in this list, there are no predefined rules as to the number allocated for each member of the list. The person who created the list also decided what number, if any, each member of the list would have. Based on this, the above code would also be written as:

Langston.TypeOfHouse = 0

 

 

Objects Methods

 

Introduction

While most objects only provide characteristics to describe them, other objects can perform actions. For example, a house can be used to protect people when it is raining outside. In computer programming, an action that an object can perform is referred to as method.

Earlier, we defined a House class with its properties. Unlike a property, a method must display parentheses on this right side to differentiate it from a property. An example would be:

House
[
      Address
      TypeOfHouse
      NumberOfBedrooms
      NumberOfBathrooms
      HasIndoorGarage
      LivingRoomCoveredWithCarpet
      KitchenHasIslandStove
      ProtectFromOutside()
]

When an object has a method, to access that method, type the name of the object, followed by a period, followed by the name of the method, and followed by parentheses. For example, if you have a House object named Langston and you want to ask it to protect its inside from outside rain, you would type:

Langston.ProtectFromOutside()

This is also referred to as calling a method.

Methods and their Arguments

When asked to perform an action, a method may need one or more values to work with. If a method needs a value, such a value is called an argument. While a certain method may need one argument, another method would need more than one. The number of arguments of a method depends on its goal. The arguments of a method are provided in parentheses.

Suppose you have a House object and you want it to protect what is inside. There may be different reasons why the inside needs to be protected: may be from the rain, may be from the windy dust, may be at night time from too much light that prevents from sleeping, etc. Based on this, you may have to provide additional information to indicate why or how the inside should be protected. For this reason, when such a method is called, this additional information must be provided, in the parentheses of the method. Here is an example:

House
[
      Address
      TypeOfHouse
      NumberOfBedrooms
      NumberOfBathrooms
      HasIndoorGarage
      LivingRoomCoveredWithCarpet
      KitchenHasIslandStove
      ProtectFromOutside(Reason)
]

As mentioned above, a method can be created to take more than one argument. In this case, the arguments are separated with commas. Here is an example:

House
[
      Address
      TypeOfHouse
      NumberOfBedrooms
      NumberOfBathrooms
      HasIndoorGarage
      LivingRoomCoveredWithCarpet
      KitchenHasIslandStove
      ProtectFromOutside(Reason, WhenToProtect)
]

The arguments are used to assist the object with performing the intended action. Once a method has been created, it can be used. Once again, using a method is referred to as calling it. If a method takes one argument, when calling it, you must provide a value for the argument, otherwise the method would not work. 

To call a method that takes an argument, type the name of the method followed by the opening parenthesis “(“, followed by the value that will be the argument, followed by a closing parenthesis “)”. The argument you pass can be a regular constant value or it can be the name of another object.

If the method is taking more than one argument, to call it, type the values for the arguments, in the exact order indicated, separated from each other by a comma.

 

Default Arguments

We have mentioned that, when calling a method that takes an argument, you must supply a value for the argument. There is an exception. Depending on how the method was created, it may be configured to use its own value if you fail, forget, or choose not, to provide one. This is known as the default argument. Not all methods follow this rule.

If a method that takes one argument has a default value for it, then you don't have to supply a value when calling that method. Such an argument is considered optional.

If a method takes more than one argument, some argument(s) may have default values while some others do not. The arguments that have default values can be used and you don't have to supply them.

We will mention default arguments when we come to a method that takes some.

 

Controls' Methods: Giving Focus

On a form that has many controls, at one particular time, only one control can receive input from the user. The control that is currently receiving input or actions from the user is said to have focus.

To give focus to a control, the user can click the intended control or press Tab a few times until the control receives focus. To programmatically give focus to a control, type the name of the control, followed by the period operator, followed by the SetFocus method. An example would be:

Private Sub Example()
    txtAddress.SetFocus
End Sub
 

Collections

 

Introduction

Imagine that you are the main real estate agent of a construction company. When the construction company has built a few houses at a particular location, your job is to find people who want to buy these houses. Although you are selling more than one house, each house is different from the others. For example, two houses cannot have the same address even if they look alike. Regardless of their differences, they are primarily considered as houses. Because of this House main characteristic they share, and because they all belong to your portfolio, these houses are considered a collection. It is important to know that what constitutes a collection is that it is made of items that use the same criteria to be described but each object has its own characteristics. For example, the fingers on your hand constitute a collection. The members of your family constitute a collection.

Access to a Collection

Like a house object in the above example, a collection must be named in order to be identified. By convention, the name of a collection is plural. For example, you can have a collection called Fingers. Another collection can be called Houses. Yet another collection can be called Cities.

Like a class, a collection has properties and methods. Like a class, a collection must be explicitly created by somebody. Although you can consider that your fingers represent a collection, to use such a collection, you must first defined the criteria that make up a collection so that each item of that collection can be identified.

On this site, we will use only collections that have already been defined. We will not have any reason to create our own collections: all the collections we will need have already been created and they are sufficient for everything we will need for our assignments.

 

Previous Copyright © 2004-2012, FunctionX Next