9.7 Write Data Validation Code That Can Be Reused in Other Classes
As you were writing the PhoneNumber and CustomerID validation code in the previous
section, you might have thought that this code would be extraordinarily useful in other
parts of the Northwind application. For example, the Suppliers and Employees tables also
have Phone and Fax fields, and the CustomerID column is also defined in the Orders
table.
Technique
In this section, you will pull the data validation code you wrote for both the
PhoneNumber and CustomerID columns and create independent objects that encapsulate
your existing validation logic. Then you will update the CCustomer class to make use of
these new classes.
The CCustomerID class will be a simple class that performs two functions: checking the
length of the ID and looking in the database to see if the CustomerID exists.
For the PhoneNumber data validation, you will learn how to create an entire object model
that will provide you with data validation for different types of phone numbers with a
bare minimum of code. And, just for the fun of it, you will learn how to use the same
base class you use to validate phone numbers to validate Social Security numbers.
Steps
1. Add a new class file to your project by right-clicking on the project in the Solution
Explorer window and selecting Add Class from the Add submenu. Name the new
class CCustomerID.vb.
2. Copy the DoesCustomerIDExist and ValidateCustomerID methods from the
CCustomer class into the CCustomerID class. Rename them Exists and Validate,
respectively.
3. Copy the InvalidCustomerIDException from CustomerClass.vb and paste it inside
the CCustomerID class. This makes your exceptions directly related to the
CCustomerID.
4. One gap in splitting off the CustomerID property into its own class is that some
parts of your application might want a read/write CustomerID property, whereas
others, such as the CCustomer class, need a ReadOnly property. Instead of having
a Boolean set to lock the property, a more flexible way is to add an event to your
End Property
7. Then add a new constructor that accepts a CustomerID as a parameter. That
constructor, shown in Listing 9.43, should call the property statement, which will
handle validation.
Listing 9.43 CCustomerID.vb: The Constructor for the CCustomerID
ClassPublic Sub New(ByVal pID As String)
Me.CustomerID = pID
End Sub
8. You're almost finished with the CCustomerID class, except for one subtle issue:
pointers. Everything you've written in this chapter so far has used base datatypes,
so you haven't had to worry about copying values between function calls. But
objects work differently than base datatypes do.
The issue is with ByVal and ByRef. With base datatypes, the distinction is fairly
straightforward: ByVal passes a copy of the value of the variable to the function.
The called function could do whatever it pleased to the passed value without
impacting the value of the variable in the calling function. ByRef passes the called
function a pointer instead of the value, so any change made to the variable in the
called function is made to the variable in the calling function.
With objects such as CCustomerID, it's completely different. Whether you use
ByVal or ByRef, you're still working with a pointer. The called function will
always modify the object that the calling function passes.
The difference is in reassigning the pointer. ByVal passes a pointer to an object. If
the called function changes the pointer to a new object, the calling function will
still point at the original object. ByRef passes a pointer to a pointer to an object. If
the called function changes the pointer to a new object, the calling function will
now point at the new object instead of the original object.
The point(er) here is in the constructors. You want to provide a way for other
developers to create copies of the class easily, or you might end up with the same
CCustomerID object being used simultaneously in a potentially conflicting
fashion. The solution? Add a constructor to the CCustomerID class that accepts a
' this constructor accepts a CCustomerID object that by
' definition is valid.
If pCustomerID.Exists Then
' Change B: removing code validating CustomerIDs
' If you recall, there used to be an additional condition
' in this If... Then that checked the length of the CustomerID.
' Now, the CCustomerID guarantees a valid CustomerID.
mdsCust = New dsCustomers()
mfNew = True
' Change C: using the object-based constructor
' Just to make sure, create a new CCustomerID object. If you
' don't, consumers of this class will retain a pointer to this
' object instance, allowing that consumer to change the value
' of the shared CCustomerID object unbeknownst to the CCustomer object.
mCustomerID = New CCustomerID(pCustomerID)
Me.CompanyName = pCompanyName
Else
' Change D: calling the CustomerID property when you need a string.
' When you throw an InvalidCustomerIDEException, you won't necessarily
' have a valid CustomerID, so this exception has to accept a string
' instead of an object. This InvalidCustomerIDException is used to express
' that the CustomerID for the new customer already exists in the database.
Throw New
CCustomerID.InvalidCustomerIDException(pCustomerID.CustomerID)
End If
End Sub
Figure 9.8. A class diagram describing the classes to be developed in section
9.7.
According to the class diagram, this class has only seven members: two variables
(cValidChars and mstrValue), one property (StringValue), three methods (IsValid,
ThrowException, and DefineValidChars), and one member class
(InvalidNumberStringException).
You might have noticed a symbol before each member declaration. This symbol
refers to the accessibility of the member. A minus sign (-) means the member is
Private, a number sign (#) means Protected, and a plus sign (+) means Public. If
this doesn't make sense at the moment, don't worry. It will be much clearer when
you see the code.
11. Because all of the classes rely on code in the base class, you should start by
defining the CNumberString class. This class will be the most complex in this
hierarchy, so you will walk through it step-by-step. First, right-click on your
project in the Solution Explorer and select Add Class from the Add submenu.
Name the class PhoneDatatypes.vb.