Classes
A class is one form of data type. As such, a class can be used in contexts where types are expected—in variable declarations, for example. In object-oriented design, classes are intended to represent the definition of real-world objects, such as customer, order, product, etc. The class is only the definition, not an object itself. An object would be a customer, an order, or a product. A class declaration defines the set of members—fields, properties, methods, and events—that each object of that class possesses. Together, these members define an object’s state, as well as its functionality. An object is also referred to as an instance of a class. Creating an object of a certain class is called instantiating an object of the class.
Consider the class definition in Example 2-4.
Example 2-4. A class definition
Public Class Employee Public EmployeeNumber As Integer Public FamilyName As String Public GivenName As String Public DateOfBirth As Date Public Salary As Decimal Public Function Format( ) As String Return GivenName & " " & FamilyName End Function End Class
The code in Example 2-4 defines a class called Employee. It has five public fields (also known as data members ) for storing state, as well as one member function . The class could be used as shown in Example 2-5.
Example 2-5. Using a class
Dim emp As New Employee( ) emp.EmployeeNumber = 10 emp.FamilyName = "Rodriguez" emp.GivenName = "Celia" emp.DateOfBirth = #1/28/1965# emp.Salary = 115000 Console.WriteLine("Employee ...
Get Programming Visual Basic .NET 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.