9.2 Create a Class That Implements the Interface You Defined
Now that you have defined the public interface of your class, you need to create a class
that will implement that interface, along with all of its methods and properties.
The place to start is with the properties of your class. You'll also need to create some
code to test your class, so you'll need to create a form that interacts with the instances of
the class.
Technique
This section uses a form with text boxes that mirror the properties of the CCustomer
class. Visual Basic .NET allows you to have classes and forms within the same .vb file,
so this section will have both in one file to make editing and debugging easier.
After setting up the form, you need to implement the class properties. If you have worked
with Property statements before, this technique will be old hat (although the syntax will
be new). If you have not worked with properties and classes, you will need the following:
•
A private variable for each property to store class data
•
Code to modify and return that data
Steps
1. Create a Windows Form and name it frmHowTo9_2. Then place text boxes for
each of the properties of the class, naming the text boxes with the property name,
prefaced by txt. For example, the postal code text box should be named
txtPostalCode. Next add command buttons called cmdDelete, cmdSave,
cmdRetrieve, and cmdNew. Finally, add a RichTextBox control called rtbToString
to contain the output of the ToString method. (This enables you to see how the
Form maps to the data of the class. See Figure 9.1)
Figure 9.1. Arrange the controls on the form you created to look like this
form.
2. Add a class declaration block to frmHowTo9_2 and name the class CCustomer.
Protected You can only access the member from classes that are derived
from (inherit from) the member's class.
Friend You can only access the member by objects within the same
project.
Protected
Friend
You can only access the member by derived classes within the
same project.
Public You can access the member by any object.
6. When you are finished, your code should look like Listing 9.10.
7. Listing 9.10 frmHowTo9_2.vb: The Empty CCustomer Class
8. Public Class Customer
9. Implements ICustomer
10.
11. Public ReadOnly Property CustomerID() As String _
12. Implements ICustomerData.CustomerID
13. Public Property CompanyName() As String Implements
ICustomerData.CompanyName
14. Public Property ContactName() As String Implements
ICustomerData.ContactName
15. Public Property ContactTitle() As String _
16. Implements ICustomerData.ContactTitle
17. Public Function Delete() As Boolean Implements ICustomerData.Delete
18. Note
Copying and pasting the code from the interface will result in code
with an invalid syntax because of the lack of End Property and
End Function/Sub lines. Although this doesn't matter for the
moment, it does disable Intellisense, which normally appears after
typing both Implements and the period after the Interface name. If
you want to enable Intellisense, press Enter at the end of each
Public Property Get PropertyName() As String
PropertyName = mstrClassVariable
End Property
Public Property Let PropertyName(ByVal strValue As String)
mstrClassVariable = strValue
End Property
In Visual Basic .NET, the property declaration has been combined in the format
shown in Listing 9.13.
Listing 9.13 Property Declarations in Visual Basic .NET
Public Property PropertyName() As String
Get
Return mstrClassVariable
End Get
Set(ByVal Value As String)
mstrClassVariable = Value
End Set
End Property
For each of your properties, add Return and the variable you declared in step 1 in
the Get block. In the Set block, type the name of a variable from step 1 and =
Value. All of your property declarations should now look like Listing 9.14.
Listing 9.14 frmHowTo9_2.vb: Some Property Declarations for the
CCustomer Class
Public ReadOnly Property CustomerID() As Customers.CCustomerID
Implements ICustomer.CustomerID
Get
Return mCustomerID
End Get
End Property