Home

Introduction to Objects

 

Classes and Objects

 

Introduction

The Microsoft Visual Basic language 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, townhouse, condominium, etc), the number of bedrooms, the number of bathrooms, etc:

 

Single Family House

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, 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.

Practical LearningPractical Learning: Introducing Objects

  1. Start Microsoft Excel
  2. On the Ribbon, click Developer
  3. In the Controls section, click Insert
  4. Under ActiveX Controls, click any object and click the main area of the spreadsheet

The Properties of an Object

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, in Microsoft Excel:

  • You can right-click the control and click Properties
  • If the control is selected in the work area, in the Controls section of the Ribbon, click the Properties button Properties

Any of these two actions would display the Properties window for the control that was right-clicked:

Properties Window

The Properties window would stay on the screen of Microsoft Excel as long as you want. To show the properties of another control, simply click it in the work area.

If you are working in Microsoft Visual Basic, to show the characteristics of a control, right-click it and click Properties. This also would display the Properties window and show the characteristics of the selected control. While the Properties window in Microsoft Excel floats and does not hold a specific position, by default, in Microsoft Visual Basic, the Properties window is position on the lower-left side.

You can move it by dragging its title bar.

Practical LearningPractical Learning: Introducing Properties

  • Right-click the object you added and click Properties

The Methods of an Object

 

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.

 
 

 

 

Techniques of Accessing the Members of an Object

 

Me

In previous lessons and sections, we saw that an object was made of properties and methods. We also saw how to access a property of an object. For example, imagine you have a House class defined as follows:

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

If you have an object named Camden and that is of type House. To access some of its properties, you would use code as follows:

Camden.Address
Camden.TypeofHouse

If you are working inside of a method of the class, for example if you are working in the body of the ProtectFromOutside method, you can also access the properties the same way, this time without the name of the object. This could be done as follows:

ProtectFromOutside()
    Address
    TypeofHouse
    NumberOfBedrooms
    NumberOfBathrooms
End

When you are accessing a member of a class inside of one of its own methods, you can precede that member with the Me object. You must include the period operator between Me and the member of the class. Here is an example:

ProtectFromOutside()
    Me.Address
    Me.TypeofHouse
    Me.NumberOfBedrooms
    Me.NumberOfBathrooms
End

Remember that the Me object is used to access the members of an object while you are inside of another member of the object.

With

We have seen that you can use the name of an object to access its members. Here is an example:

Camden.Address
Camden.TypeOfHouse
Camden.NumberOfBedrooms
Camden.NumberOfBathrooms
Camden.HasIndoorGarage

Instead of using the name of the object every time, you can start a section with the With keyword followed by the name of the object. In another line, end the section with the End With expression:

With Camden
    
End With

Between the With and the End With lines, to access a member of the class that the object is built from, type a period followed by the desired member. This would be done as follows:

With Camden
    .Address
    .TypeOfHouse
    .NumberOfBedrooms
    .NumberOfBathrooms
    .HasIndoorGarage
End With

As you access a member, you can perform on it any action you judge necessary.

Design Time and Run Time

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. The time you are designing an application is referred to as design time. The time the application (form) displays to the user is referred to as run time.

You can manipulate the characteristics of a control both at design and at run times. This means that you can set some properties at design time and some others at run time.

Practical Learning: Closing Microsoft Excel

  1. To close Microsoft Excel, click the Office Button and click Exit Excel
  2. When asked whether you want to save the file, click No
 
 
   
   

Previous Copyright © 2008-2016, FunctionX, Inc. Next