Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

406 Part III Designing the User Interface


To create a method in the class that performs a specific action, you add a function
or a Sub procedure to your class. Although many methods don’t require arguments
to accomplish their work, the Age method I’m defining requires a Birthday argument
of type Date to complete its calculation. The method uses the Subtract method to
subtract the new employee’s birth date from the current system time, and it returns
the value expressed in days divided by 365 .25—the approximate length in days of
a single year. The Int function returns the integer portion of a number, and this value
is returned to the calling procedure via the Return statement—just like a typical
function. (For more information about function definitions, see Chapter 10, “Creating
Modules and Procedures .”)
Your class definition is finished, and in the Code Editor, the Person class now looks like
the following:

Now you’ll return to Form1 and use the new class in an event procedure.

Tip Although you didn’t do it for this example, it’s usually wise to add some type-checking
logic to class modules in actual projects so that properties or methods that are improperly
used don’t trigger run-time errors that halt the program.
Free download pdf