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

 

With

 

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.

Arrays

 

Introduction

 

For a Set of Value

 

Introduction

 

For Each Member of the Set

 

 

 

Previous Copyright 2004-2008 Yevol Next