Microsoft 9GD00001 Computer Accessories User Manual


 
Chapter 12: Customizing the Development Environment 361
Return newCustID
End Function
End Class
The point of interest in the AddNewCustomer method is the firstName and lastName
parameters. Whenever working with data, you’ll usually want to ensure that input data is
legal. When user input is being processed, it’s common to get bad information, even if you
have good input validation in your user interface code. For example, the following code
calls the preceding AddNewCustomer method, passing in bad data as arguments:
C#:
class Program
{
static void Main()
{
string firstName = "Joe";
string lastName = null;
Customer cust = new Customer();
cust.AddNewCustomer(firstName, lastName);
}
}
VB:
Module Module1
Sub Main()
Dim firstName As String = "Joe"
Dim lastName As String = Nothing
Dim cust As New Customer
cust.AddNewCustomer(firstName, lastName)
End Sub
End Module
In the preceding example, firstName is okay because it has a good name in it.
However, notice that lastName is set to null (Nothing in VB). This would cause
a NullReferenceException if AddNewCustomer tried to call a string operation on
the parameter, the code that AddNewCustomer calls could potentially throw a
NullReferenceException, or (assuming that null is considered invalid in this case) you