Recipe 3.2. Creating a Class


Problem

You need to add a new class to your Visual Basic application.

Solution

To add a new project class to your application, select the Project Add Class menu command, and complete the Add New Item dialog.

Discussion

The Add New Item dialog, shown in Figure 3-2, prompts you by selecting the Class template.

Figure 3-2. Adding a new class in Visual Studio


Give your class a new name, and then click the Add button. Visual Basic displays your newly added class in a code editor window. For example, providing "Class1.vb" for the new class filename adds the class source-code file and displays the following empty class template:

 Public Class Class1 End Class 

Of the various object types included with Visual Basic ( classes, structures, and modules), classes have the most flexibility and the least restrictions on use. You can add pretty much any supported member type, including Sub procedures, functions, fields, constants, enumerations, events, delegates, other classes and structures, and proper-ties. Here is a simple class that uses many of those features:

 Public Class Employee    ' ----- Basic employee information fields.    Public LastName As String    Public FirstName As String    Public HireDate As Date    Public JobType As EmployeeJobType    Private CurrentSalary As Decimal    ' ----- Supplies values to the JobType public field.    Public Enum EmployeeJobType       CLevel       Manager       NonManager       Contractor    End Enum    ' ----- Used by the SalaryChanged event arguments.    Public Class SalaryChangedEventArgs       Inherits System.EventArgs       Public OldSalary As Decimal       Public NewSalary As Decimal    End Class    ' ----- Argument signature for the SalaryChanged event.    Public Delegate Sub SalaryChangedDelegate( _       ByVal sender As Object, _       ByVal e As SalaryChangedEventArgs)    ' ----- Issued when private CurrentSalary field changes.    Public Event SalaryChanged As SalaryChangedDelegate        Public Function GetFullName( ) As String       ' ----- Return a nicely formatted name.       Return FirstName & " " & LastName    End Function    Public Sub GiveRaise(ByVal percentIncrease As Decimal)       ' ----- To raise 10%, set percentIncrease to 0.10.       Dim changeDetail As New SalaryChangedEventArgs       ' ----- Record the new salary, keeping track       '       of the change.       changeDetail.OldSalary = CurrentSalary       CurrentSalary += (CurrentSalary * percentIncrease)       changeDetail.NewSalary = CurrentSalary       ' ----- Inform anyone who may be interested.       RaiseEvent SalaryChanged(Me, changeDetail)    End Sub    Public Property Salary( ) As Decimal       Get          ' ----- Report the current salary level.          Return CurrentSalary       End Get       Set(ByVal value As Decimal)          ' ----- Update the private CurrentSalary field.          Dim changeDetail As New SalaryChangedEventArgs          ' ----- Ignore negative salaries.          If (value < 0@) Then Exit Property          ' ----- Record the new salary, keeping track          '       of the change.          changeDetail.OldSalary = CurrentSalary          CurrentSalary = value          changeDetail.NewSalary = value          ' ----- Inform anyone who may be interested.          RaiseEvent SalaryChanged(Me, changeDetail)       End Set    End Property End Class 

One source-code file may include multiple classes, structures, and modules:

 Class Class1    ' ----- First class members go here. End Class Class Class2    ' ----- Second class members go here. End Class 

If you attempt this in a form class file, the Visual Studio Form Designer looks only at the first class in the file. If you insert a class (or structure or module) before the form-derived class in the file, Visual Studio can't display the form.

Classes are the basic building blocks of Visual Basic applications. The two other major types structures and modulesare variations of the basic class type, with certain restrictions that make them useful in certain cases.

The code for a class usually appears in a source-code file all its own, although you can divide a class into multiple files (see Recipe 3.9). You can also store multiple classes in a single source-code file, but this can quickly clutter your code.

See Also

Recipes 3.1 and 3.3 introduce modules and structures, the two other major type constructs in Visual Basic.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

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