| < Day Day Up > |
A class is used to define an object. It is a
Statements within the class comprise its methods, properties, and events. Members declared as Private are available only within the class. Public
Methods in VB .NET are created using the
Sub
or
Function
keywords. A method created with
Sub
does not return a value, whereas a
Function
must return a value as a result. We've already looked at the concepts of a
Function
and
Sub
, so we won't
Private: Only visible within the class
Friend: Visible by code within the project
Public: Visible by code outside the class
Protected: Available to subclasses
Protected Friend: Visible to the project and by code in subclasses
Properties store information in an object and can be specified by either public variables or as property methods. When properties are declared using a public variable, they are also referred to as fields. This method does not provide the ability to control (validate) reading and writing a property. Property methods allow you to control read and write operations of properties in a class using the ReadOnly keyword.
For example, let's look again at the Employee object we have talked about in pseudocode throughout this chapter. We'll create a simple class:
Class Employee Public ID As Integer = 1 Public Property Salary() As Integer ' This is a Property. Get Return Salary End Get Set(ByVal Value As Integer) NumWheelsValue = Value End Set End Property End Class
This doesn't take into account everything we would need to include in a real Employee object, such as name and so on, but the concepts for creating them are exactly the same as the previous code. We begin by creating a Public Property called Salary and then use Get/Set to retrieve or set the property value in our code. For example, after the class is available in the project, you can create a new Employee object as follows:
Dim clsEmployee As New Employee()
Then, you could assign a salary to an employee as follows:
clsEmployee.Salary = 500
We could also add methods to the example class:
Class Employee Public ID As Integer = 1 Public Property Salary() As Integer ' This is a Property. Get Return Salary End Get Set(ByVal Value As Integer) NumWheelsValue = Value End Set End Property Public Sub IncreaseSalary() 'We need code for increasing salary End Sub End Class
Again, these are very simple examples. Don't worry if all of this doesn't click at this time. We review these types of ideas as we use them in the book in real examples.
| < Day Day Up > |
| < Day Day Up > |
Constructors are
Back to our employee class concept, we could create a constructor as
Public Sub New() Salary = 500 End Sub
Using this example, when the class is
We could also allow the
Public Sub New(ByVal InitialSalary As Integer) Salary = InitialSalary End Sub
The user of the class could then create an object as follows:
Dim clsEmployee as New Employee (500)
This works well, but suppose that we want to offer a third option for the user of the class. Instead of assigning the salary by default to a value of 500 , or using a constructor that required the user to assign it, let's use the Optional keyword to create an optional constructor:
Public Sub New(Optional ByVal InitialSalary As Integer = 500) Salary = InitialSalary End Sub
Now, if the user
Although these have been very simple examples, you can see how constructors can be utilized in your applications.
| < Day Day Up > |