Chapter 27. Fine-Tuning Classes
In Lesson 26 you learned how to build constructors, special methods that execute when an object is created. In this lesson you learn about other special methods you can give a class. In particular, you learn how to overload and override class methods.
OVERLOADING METHODS
Lesson 26 mentioned that you can give a class any number of constructors as long as they have different parameter lists. For example, it's common to give a class a parameterless constructor that takes no parameters, and one or more other constructors that take parameters.
Making multiple methods with the same name but different parameter lists is called overloading. Visual Basic uses the parameter list to determine which version to use when you invoke the method.
For example, suppose you're building a course assignment application and you have built Student, Course
, and Instructor
classes. You could give the Student
class two versions of the Enroll
method, one that takes as a parameter the name of the class in which the student should be enrolled and a second that takes a Course
object as a parameter.
You could give the Instructor
class similar versions of the Teach
method to make the instructor teach a class by name or Course
object.
Finally, you could give the Course
class different Report
methods that:
Display a report in a dialog if there are no parameters
Append a report to the end of a file if the method receives a
FileStream
as a parameterSave the report into a new file if the method ...
Get Stephens' Visual Basic® Programming 24-Hour Trainer now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.