Section 9.8. Using the Me Reference to Access the Current Object


9.8. Using the Me Reference to Access the Current Object

Every object of a class shares the class's method declarations. We have seen that an object's methods can manipulate the object's data. But how do methods know which object's instance variables to manipulate? Every object has access to itself through a reference called Me (a Visual Basic keyword). On every method call, the compiler passes an object's Me reference as an implicit argument to each of the object's non-Shared methods. The Me reference can then be used to access a particular object's members implicitly or explicitly. (Section 9.10 introduces Shared class members and explains why the Me reference is not implicitly passed to Shared methods.)

Class Time (Fig. 9.8) defines Private instance variables hour, minute and second (lines 46). The constructor (lines 914) receives three Integer arguments to initialize a Time object. For this example, we use parameter names (lines 910) that are identical to the class's instance variable names (lines 46). When a method has a parameter or local variable with the same name as one of the class's instance variables, the instance variable is shadowed (hidden) in the method's scope. However, the method can use the Me reference to refer explicitly to shadowed instance variables. Lines 1113 of Fig. 9.8 demonstrate this feature.

Method BuildString (lines 1720) returns a String created by a statement that uses the Me reference first explicitly then implicitly. Line 18 uses the Me reference explicitly to call method ToUniversalString, whereas line 19 calls the same method using the Me reference implicitly. Note that both lines generate identical outputs.

Figure 9.8. Class using the Me reference.

  1  ' Fig. 9.8: Time.vb  2  ' Encapsulate time using Me reference.  3  Class Time  4     Private hour As Integer  5     Private minute As Integer  6     Private second As Integer  7  8     ' Time constructor  9     Public Sub New(ByVal hour As Integer, _ 10        ByVal minute As Integer, ByVal second As Integer) 11        Me .hour = hour ' set instance variable to parameter     12        Me .minute = minute ' set instance variable to parameter 13        Me .second = second ' set instance variable to parameter 14     End Sub ' New 15 16     ' create String using Me explicitly then implicitly 17     Public Function BuildString() As String 18        Return "Me.ToUniversalString(): " & Me.ToUniversalString() _ 19           & vbCrLf & "ToUniversalString(): " & ToUniversalString() 20     End Function ' BuildString 21 22     ' convert to String in universal-time format 23     Public Function ToUniversalString() As String 24        Return String.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second) 25     End Function ' ToUniversalString 26  End Class ' Time 

Error-Prevention Tip 9.2

For a method in which a parameter has the same name as an instance variable, use reference Me to access the instance variable explicitly; otherwise, the method parameter is referenced.


Error-Prevention Tip 9.3

Avoid parameter names that conflict with instance variable names to prevent subtle, hard-to-find bugs.


Module MeTest (Fig. 9.9) demonstrates the Me reference. Line 5 instantiates a Time object. Line 6 invokes method BuildString, then displays the results to the user.

Figure 9.9. Me reference demonstration.

 1  ' Fig. 9.9: MeTest.vb 2  ' Demonstrates Me reference. 3  Module MeTest 4     Sub Main() 5        Dim time As New Time(12, 30, 19) 6        Console.WriteLine(time.BuildString()) 7     End Sub ' Main 8  End Module ' MeTest 

 Me.ToUniversalString(): 12:30:19 ToUniversalString(): 12:30:19 





Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net