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 Scores() As Integer
    ...
    Public Sub CompareToStudent(other_student As Student)
        Dim my_score As Integer = 0
        For i As Integer = 0 To Scores.GetUpperBound(0)
            my_score += Scores(i)
        Next i
 
        Dim other_score As Integer = 0
        For i As Integer = 0 To other_student.Scores.GetUpperBound(0)
            other_score += other_student.Scores(i)
        Next i
 
        Debug.WriteLine("My score: " & my_score)
        Debug.WriteLine("Other score: " & other_score)
    End Sub
    ...
End Class

Breaking the encapsulation provided by the objects in this way can lead to unnecessary confusion. It is generally better to try to access an object’s Private data only from within that object. You can provide access routines that make using the object’s data easier.

The following version of the Student class includes a TotalScore function that returns the total of a Student object’s scores. This function works only with its own object’s scores, so it does not pry into another object’s data. The CompareToStudent subroutine uses the TotalScore function to display the total score for its object and for a comparison object.

Public Class Student
    Public FirstName As String
    Public LastName As String
    Private Scores() As Integer
    ...
    Public Sub CompareToStudent(other_student As Student)
        Debug.WriteLine("My score: " & TotalScore())
        Debug.WriteLine("Other score: " & other_student.TotalScore())
    End Sub
 
    ' Return the total of this student's scores.
    Private Function TotalScore() As Integer
        Dim total_score As Integer = 0
        For i As Integer = 0 To Scores.GetUpperBound(0)
            total_score += Scores(i)
        Next i
 
        Return total_score
    End Function
    ...
End Class

Function TotalScore is itself declared Private, so only code within the class can use it. In this example, the CompareToStudent subroutine calls another object’s Private TotalScore function, so the separation between the two objects is not absolute, but at least CompareToStudent doesn’t need to look directly at the other object’s data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.117.4