3.3. Designing Your Own Classes


While the My objects and the hundreds of types in the .NET Framework Class Library provide ready-to-use solutions to many of the tasks you need to perform in a Visual Basic application, at some point you'll need to create your own classes. In this section, you'll see how to do that. You'll also see how you can control access to the variables and methods in your classes, and when it might make sense to use a Structure a "light-weight" class instead.

3.3.1. Defining a Class

Before you build a car, you need a design. The design of a car specifies its properties, its behaviors, and how it works internally. Likewise, to design a class of your own, you need to specify the methods and properties, as well as the internal workings of the class.

To see how to define a class, let's create a Stack class of our own. In the following steps, you'll use the Visual Studio 2005 class designer to get the work done.

  1. Using Visual Studio 2005, create a new Windows application. Name the project StackClassApp.

  2. Add a new class file to the project by right-clicking on the project name in Solution Explorer and then selecting Add New Item…. Select the Class template and name it Figure 3-9. Adding a new class file to the project


  3. The StackClass.vb file will now be opened in Visual Studio 2005, ready to be edited. You can either define your class by coding directly into the open StackClass.vb file, or use the Class Designer to do the job. Let's first take a look at the Class Designer.

    The Class Designer is not included in the Visual Basic 2005 Express Edition. If you are using Visual Basic 2005 Express Edition, please skip to Step 6.


  4. To use the Class Designer, right-click on StackClass.vb in Solution Explorer and then select View Class Diagram, as shown in Figure 3-10.

    Figure 3-10. Invoking the Class Designer


  5. The Class Designer will display a rectangular box showing the empty StackClass class. To add methods, properties, and more to the class, right-click on the class rectangle and select Add, as shown in Figure 3-11, and select the elements you wish to add.

    Figure 3-11. Modifying the class


    Figure 3-12 shows what the completed class diagram for StackClass might look like after you've added the methods, properties, and other elements called for in your design. You can use the Properties window to customize each element (such as access mode, data type, etc.). However, when defining a relatively simple class, like the StackClass, it is faster to type the code in directly, as you'll do in the next step. The Class Designer is useful when you have several classes and you want to view their relationship visually.

    Figure 3-12. Adding the various methods, properties, and fields to the StackClass class


  6. Let's abandon the Class Designer, open StackClass.vb, and simply type the code shown in Example 3-9 into the file.

Example 3-9. The definition for the StackClass class
 Public Class StackClass '---stores the items in the stack Private element() As Object '---indicate the current stack pointer Private pointer As Integer '---instantiates and specify the default size of the stack Public Sub New() ReDim element(100) pointer = 0 End Sub '---instantiates and specify the size of the stack Public Sub New(ByVal size As Integer) ReDim element(size - 1) pointer = 0 End Sub '---push an item into the stack Public Sub Push(ByVal item As Object) If pointer > UBound(element) Then Throw New Exception("Stack is full.") End If element(pointer) = item pointer += 1 End Sub '---pop an item from the stack Public Function Pop() As Object pointer -= 1 If pointer < 0 Then Throw New Exception("Stack is empty.") End If Return element(pointer) End Function '---return the number of items in the stack ReadOnly Property count() As Integer Get Return pointer End Get End Property End Class 

Observe that StackClass contains the following elements:

  • Two private variables (element and pointer) that are used internally to store the items in a stack. The Private keyword indicates that the variables are visible only within the class and cannot be accessed outside the class.

  • Two constructors (New) that initialize the object when it is instantiated.

  • Two methods (a subroutine and a function) for pushing (Push) and popping (Pop) items in and out of the stack. The Public keyword indicates that the methods are accessible outside the class.

  • One read-only property that returns the number of items in the stack.

3.3.2. Controlling Access to Class Members

Variables, classes, and members can be declared to be public or private using the Public and Private access modifiers. Access modifiers restrict the scope of member variables in a class. For example, a variable defined with the Private keyword is visible only within the class in which it is defined. A Public variable, on the other hand, is visible outside the class. Declaring a private variable is useful in cases where you do not want users who are using your class to know about the detailed workings of your class.

There are two more access modifiers that you can use:

  • Protected

  • Friend

To see how these two access modifiers affect the scope of variables, classes, and member variables, consider the following example.

Suppose you have the following class definition:

 Public Class BMW Friend var1 As Integer Protected var2 As Integer Private var3 As Integer Public var4 As Integer End Class 

Within the class, you have four variables each declared with a different access modifier. Create an instance of class BMW and try to assign values to the member variables:

 Dim objA As New BMW objA.var1 = 1 objA.var2 = 2 ' Error; not allowed objA.var3 = 3 ' Error; not allowed objA.var4 = 4 

You will notice that var2 and var3 are not accessible because:

  • var2 is declared with the Protected access modifier. The Protected access modifier works like the Private access modifier, which means that the variable is not visible outside the class. However, the difference between Protected and Private is that variables declared Protected are visible within their own class or subclasses. You will see more of this in the next example.

  • var3 is a private variable.

Consider the following example, where class MiniCooper inherits from class BMW:

 Public Class MiniCooper Inherits BMW Public Sub doSomething() MyBase.var1 = 1 MyBase.var2 = 2 MyBase.var3 = 3 'Error; not allowed MyBase.var4 = 4 End Sub End Class 

Within class MiniCooper, you have a method doSomething that tries to access the four variables in the base class. Notice that var2 is accessible but var3 is not. This is because:

  • var2 is visible within the subclasses of class BMW. Hence, var2 is accessible.

  • var3 is a private variable.

In the two examples above, you may notice that var1 is visible all along. Basically, the Friend and the Public access modifiers are similar with the exception that Friend variables are accessible from within their declaration context and from anywhere else in the same program (but not outside the program).

You can combine the two access modifiers, Protected and Friend, together to give a variable both protected and friend access. For example, here var2 is now declared as Protected Friend:

 Public Class BMW Friend var1 As Integer Protected Friend var2 As Integer Private var3 As Integer Public var4 As Integer End Class 

This means that var2 is visible within the subclass and also visible within the same program. The following code example shows that now var2 is visible within the same program:

 Dim objA As New BMW objA.var1 = 1 objA.var2 = 2 ' Allowed! objA.var3 = 3 ' Error; not allowed objA.var4 = 4 

3.3.3. Aggregating Data Types Using a Structure

Sometimes you need to represent a piece of information using multiple data types, but don't necessarily want the overhead of defining a class and managing an object. For example, suppose you need to maintain information about the different types of car owned by a company, such as model and year of registration. In this case, you can either use a class or a structure to aggregate all the required information.

In VB 2005, a structure is implemented using the Structure keyword. In VB 6, you define structure using the Type…End Type syntax, which is no longer supported in VB 2005.

Example 3-10 shows the definition for a Structure named Car.

Example 3-10. Declaring a structure
 Structure Car Public Model As String Public Year As UShort End Structure 

Note that in a Structure, you can define properties and methods just like in a class.


To use a Structure, you simply declare variables to be of the structure type, as shown in Example 3-11.

Example 3-11. Using structures
 Dim Car1, Car2 As Car Car1.Model = "Nissan Maxima"  Car1.Year = 2004 Car2 = Car1  Car2.Model = "Toyota Camry" 

Example 3-11 creates two variables of type Car. The first variable is initialized and then copied to the second variable. Because structure is a value type, changes to the second variable do not affect the first member (see Figure 3-13).

Figure 3-13. Memory storage for a structure


So, what is the difference between a class and a structure? A class is a reference type, which means that the actual storage of an object is on the heap with the object variable on the stack pointing to it. A structure, on the other hand, is a value type, and its value is stored directly on the stack.

You should use a structure when:

  • You have a small amount of data.

  • You perform a large number of operations on each instance; in this case, performance is much faster than using a class.

  • You have no need to inherit the structure.

A class is preferred when:

  • You need to use inheritance for complex data types.

  • You need to initialize one or more members at creation time.



Visual Basic 2005 Jumpstart 2005
Visual Basic 2005 Jumpstart
ISBN: 059610071X
EAN: 2147483647
Year: 2005
Pages: 86
Authors: Wei-Meng Lee

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net