CONSTANTS, PROPERTIES, AND METHODS

The way you declare constants, properties, and methods within a class is the same as the way you declare them outside a class. The main difference is that the context of the declaration is the class rather than a namespace. For example, a variable declared Private within a class is available only to code within the class.

For information on declaring variables and constants, see Chapter 14, “Data Types, Variables, and Constants.” For information on declaring methods, see Chapter 16, “Subroutines and Functions,” which also describes property procedures, special routines that implement a property for a class.

One issue that is sometimes confusing is that the unit of scope of a class is the class’s code, not the code within a specific instance of the class. If you declare a variable within a class Private, then all code within the class can access the variable, whether or not that code belongs to the instance of the object that contains the variable.

For example, consider the following Student class. The Scores array is Private to the class, so you might think that a Student object could only access its own scores. In fact, any Student object can access any other Student object’s Scores array as well. The CompareToStudent subroutine calculates the total score for the current Student object. It then calculates the total score for another student and displays the results.

Public Class Student Public FirstName As String Public LastName As String Private ...

Get Visual Basic 2012 Programmer's Reference 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.