Flylib.com

Books Software

 
 
 

Developing Tablet PC Applications (Charles River Media Programming) - page 37

 < Day Day Up > 


Classes

A class is used to define an object. It is a sort of template that you can use to define the properties and methods of your objects and is the structure that can contain events, constants, and variables . To create a class, you begin with the Class keyword and assign a name to create instances of the class. Any time we need to create a class in VB .NET, we simply put all the code for the class within the Class...End Class block. This is similar in many ways to the loops we looked at in the Chapter 5, Introduction to the VB .NET Language.

Statements within the class comprise its methods, properties, and events. Members declared as Private are available only within the class. Public members , on the other hand, are available outside the class and are the default declaration if neither is specified.

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 spend any additional time on them. You can declare methods as follows :

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

Constructors are methods of a class that are executed when a class is instantiated . They are very often used to initialize the class. To create a constructor, you simply add a public procedure called New() to your class. You can also use a parameterized constructor. This allows you to create a class that can have parameters passed to it when it is called.

Back to our employee class concept, we could create a constructor as follows :

Public Sub New() Salary = 500 End Sub

Using this example, when the class is initialized , we set the salary to a value of 500 . This doesn't mean that it cannot be changed, but instead of assigning every employee a salary, they would begin with a salary of 500 that could be added to, subtracted from, or just changed completely.

We could also allow the user of the object to the constructor to pass in the initial value instead of assigning it to 500 . We could use a parameterized constructor to do this. We add a parameter for the InitialSalary as follows:

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 assigns a value, it overrides the default 500 value. If a value is not passed, the value is the default of 500 .

Although these have been very simple examples, you can see how constructors can be utilized in your applications.



 < Day Day Up >