Home

Objects and Collections

 

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.

A Method of a Class

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

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

Intead of using the name of the object everytime, 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.

Fundamentals of Collections

 

Introduction

A collection is a series of items where each item has the same characteristics. In order words, all items can be described the same way. Programmatically, a collection is a series of items where all items share the same properties and methods, if any. For example, a collection can be made of employees of a company where each employee can be described with a characteristics such as a name.

Creating a Collection

In our lessons, we will not create new collections. We will only use two categories: the Collection class and the built-in collection.

To support collections, the Visual Basic language is equipped with a class named Collection.

Author Note Actually, the Collection class we are going to study here is the one defined in VBA. The parent Visual Basic language has a somewhat different Collection class with additional functionality not found in the VBA's version.

This class can be used to create a collection. To do this, declare a variable of type Collection. Here is an example:

Sub Exercise()
    Dim Employees As Collection
End Sub

After declaring the variable, to allocate memory for it, use the Set operator to assign a New instance to the variable. Here is an example:

Sub Exercise()
    Dim Employees  As Collection
    
    Set Employees = New Collection
End Sub

Instead of always creating a new collection unless you have to, VBA for Microsoft Excel comes equipped with many collections so that you almost may never need to create your own collection. The collections that are already built in the VBA language are referred to as built-in collections.

The built-in collection classes are derived from the Visual Basic's Collection class. As a result, all of their primary functionality comes from the Collection class. This also means that everything we will mention for the Collection class applies to any built-in collection.

To use a built-in collection, you can declare a variable for it. Here is an example:

Sub Exercise()
    
    Dim CurrentSheets As Worksheets
    
End Sub

In reality, and as we will next in the next lessons, when Microsoft Excel starts, most (if not all) of the built-in collection classes are already available so that you do not have to declare their variable before using them.

Characteristics of, and Operations on, a Collection

 

Adding an Item to a Collection

The primary operation to perform on a collection consists of adding items to it. To support this, the Collection class is equipped with a method name Add. Its syntax is:

Public Sub Add( _
   ByVal Item As Object, _
   Optional ByVal Key As String, _
   Optional ByVal { Before | After } As Object = Nothing _
)

This method takes three arguments. Only the first is required. The Item argument specifies the object to be added to the collection. Here is an example:

Sub Exercise()
    Dim Employees  As Collection
    
    Set Employees = New Collection
    
    Employees.Add "Patricia Katts"
End Sub

In the same way, you can add as many items as you want:

Sub Exercise()
    Dim Employees  As Collection
    
    Set Employees = New Collection
    
    Employees.Add "Patricia Katts"
    Employees.Add "James Wiley"
    Employees.Add "Gertrude Monay"
    Employees.Add "Helene Mukoko"
End Sub

Remember that if you are using one of the built-in collection classes, you do not have to declare a variable for it. You can just call the Add method on it to add an item to it. Here is an example:

Sub Exercise() 
    Worksheets.Add
End Sub

Accessing an Item in a Collection

The items of a collection are organized in an arranged sequence where each item holds a specific index. the first item in the collection holds an index of 1. The second item holds an index of 2, and so on.

To give you access to the items of a collection, the Collection class is equipped with a property named Item. There are two ways you can use this property.

To formally use the Item property, type the name of the collection object, followed by the period operator, followed by Item and optional parentheses. After the Item property or inside its parentheses, type the index of the desired item. Here is an example:

Sub Exercise()
    Dim Employees  As Collection
    
    Set Employees = New Collection
    
    Employees.Add "Patricia Katts"
    Employees.Add "James Wiley"
    Employees.Add "Gertrude Monay"
    Employees.Add "Helene Mukoko"
    
    Employees.Item 2
End Sub

Remember that you an also use parentheses:

Sub Exercise()
    Dim Employees  As Collection
    
    Set Employees = New Collection
    
    Employees.Add "Patricia Katts"
    Employees.Add "James Wiley"
    Employees.Add "Gertrude Monay"
    Employees.Add "Helene Mukoko"
    
    Employees.Item (2)
End Sub

Instead of using the Item property, you can apply the index directly to the collection object. Here are examples:

Sub Exercise()
    Dim Employees  As Collection
    
    Set Employees = New Collection
    
    Employees.Add "Patricia Katts"
    Employees.Add "James Wiley"
    Employees.Add "Gertrude Monay"
    Employees.Add "Helene Mukoko"
    
    Employees.Item 2
    Employees.Item (2)
    
    Employees 2
    Employees (2)
End Sub

All these four techniques (notations) give you access to the item whose index you provided.

Removing an Item From a Collection

As opposed to adding a new item, you can delete one. To support this operation, the Collection class is equipped with a method name Remove. Its syntax is:

Public Sub Remove(Index As Integer)

This method takes one argument. When calling it, pass the index of the item you want to delete. Here is an example:

Sub Exercise()
    Dim Employees  As Collection
    
    Set Employees = New Collection
    
    Employees.Add "Patricia Katts"
    Employees.Add "James Wiley"
    Employees.Add "Gertrude Monay"
    Employees.Add "Helene Mukoko"
    
    Employees.Remove 2
End Sub

This code deletes the second item in the collection.

The Number of Items in a Collection

When you start a new collection, obviously it is empty and its number of items is 0. To keep track of the number of items in a collection, the Collection class is equipped with a property named Count whose type is an integer. Remember that all built-in collection classes inherit their behavior from the Collection class. This means that the built-in collection classes are equipped with a property named Count to hold their number of items.

We saw how you can add new items to a collection. Every time you add a new item to the collection, the Count property increases by 1.

We also know how to remove an item from a collection. Whenever an existing item is deleted, the value of the Count proeprty is decreased by 1.

At anytime, to know the number of items that a collection is currently holding, get the value of its Count property.

 

Previous Copyright 2008 Yevol.com Next