VB-101 "Me"

G

Guest

Hi,
Maybe someone can explain the use of "Me" to me?
The example class CPoint3 uses the Me reference in the output statement:
Console.WriteLine("CPoint3 constructor: {0}", Me)

The text states: "This implicitely invokes the class's ToString method".
This would be clear if Me.ToString was used, which I tested and works too!.
Actually, Me.Anything works and gives the same results as long as Anything
is a class member.

The class contains 4 methods (2 constructors, Finalize and ToString, two
properties).
What does the Me reference in the output statement exactly all invoke?
1. Does it call all methods but does only the ToString method lead to
printed text since it generates a string representation?
2. The Me reference cannot invoke other methods than the ToString method?
(Does not sound likely!)

Thanks for your explanation,

John

*******
' Fig. 9.16: Point3.vb
' CPoint3 class represents an x-y coordinate pair.

Public Class CPoint3

' point coordinate
Private mX, mY As Integer

' default constructor
Public Sub New()

' implicit call to Object constructor occurs here
X = 0
Y = 0
Console.WriteLine("CPoint3 constructor: {0}", Me)
End Sub ' New

' constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer)

' implicit call to Object constructor occurs here
X = xValue
Y = yValue
Console.WriteLine("CPoint3 constructor: {0}", Me)
End Sub ' New

' finalizer overrides version in class Object
Protected Overrides Sub Finalize()
Console.WriteLine("CPoint3 Finalizer: {0}", Me)
MyBase.Finalize() ' call Object finalizer
End Sub ' Finalize

' property X
Public Property X() As Integer

Get
Return mX
End Get

Set(ByVal xValue As Integer)
mX = xValue ' no need for validation
End Set

End Property ' X

' property Y
Public Property Y() As Integer

Get
Return mY
End Get

Set(ByVal yValue As Integer)
mY = yValue ' no need for validation
End Set

End Property ' Y

' return String representation of CPoint3
Public Overrides Function ToString() As String
Return "[" & mX & ", " & mY & "]"
End Function ' ToString

End Class ' CPoint3
 
G

Guest

All objects in .NET derive from the .NET base class "Object". Object
implements the ToString method and , in your case, the CPoint3 class
overrides the base class's implementation. Many Windows functions allow you
to pass any object into it. (Including a string). When you pass an object,
the Window's function will typically use the ToString method to display the
actual data that represents the object. (Since all objects have this method
because they derive from the .NET base class "Object").
 
G

Guest

Thanks TrtnJohn,
Yes, I understand there are 8 methods defind by class object (and that
method ToString is one of them) and that every derived class inherits these 8
methods.

I still don't quite understand how the Me reference works. What is the
difference between Me.methodName and only Me? Does Me.methodName only call
the method methodName and does Me (alone) invoke only (but any of) the 8
methods of class object if they are listed in class CPoint3?
What if there are more methods of class object present in class CPoint3, are
all of them executed?

Thanks for your help,

Regards,

John


TrtnJohn said:
All objects in .NET derive from the .NET base class "Object". Object
implements the ToString method and , in your case, the CPoint3 class
overrides the base class's implementation. Many Windows functions allow you
to pass any object into it. (Including a string). When you pass an object,
the Window's function will typically use the ToString method to display the
actual data that represents the object. (Since all objects have this method
because they derive from the .NET base class "Object").

John said:
Hi,
Maybe someone can explain the use of "Me" to me?
The example class CPoint3 uses the Me reference in the output statement:
Console.WriteLine("CPoint3 constructor: {0}", Me)

The text states: "This implicitely invokes the class's ToString method".
This would be clear if Me.ToString was used, which I tested and works too!.
Actually, Me.Anything works and gives the same results as long as Anything
is a class member.

The class contains 4 methods (2 constructors, Finalize and ToString, two
properties).
What does the Me reference in the output statement exactly all invoke?
1. Does it call all methods but does only the ToString method lead to
printed text since it generates a string representation?
2. The Me reference cannot invoke other methods than the ToString method?
(Does not sound likely!)

Thanks for your explanation,

John

*******
' Fig. 9.16: Point3.vb
' CPoint3 class represents an x-y coordinate pair.

Public Class CPoint3

' point coordinate
Private mX, mY As Integer

' default constructor
Public Sub New()

' implicit call to Object constructor occurs here
X = 0
Y = 0
Console.WriteLine("CPoint3 constructor: {0}", Me)
End Sub ' New

' constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer)

' implicit call to Object constructor occurs here
X = xValue
Y = yValue
Console.WriteLine("CPoint3 constructor: {0}", Me)
End Sub ' New

' finalizer overrides version in class Object
Protected Overrides Sub Finalize()
Console.WriteLine("CPoint3 Finalizer: {0}", Me)
MyBase.Finalize() ' call Object finalizer
End Sub ' Finalize

' property X
Public Property X() As Integer

Get
Return mX
End Get

Set(ByVal xValue As Integer)
mX = xValue ' no need for validation
End Set

End Property ' X

' property Y
Public Property Y() As Integer

Get
Return mY
End Get

Set(ByVal yValue As Integer)
mY = yValue ' no need for validation
End Set

End Property ' Y

' return String representation of CPoint3
Public Overrides Function ToString() As String
Return "[" & mX & ", " & mY & "]"
End Function ' ToString

End Class ' CPoint3
 
D

dgk

Hi,
Maybe someone can explain the use of "Me" to me?
The example class CPoint3 uses the Me reference in the output statement:
Console.WriteLine("CPoint3 constructor: {0}", Me)

The text states: "This implicitely invokes the class's ToString method".
This would be clear if Me.ToString was used, which I tested and works too!.
Actually, Me.Anything works and gives the same results as long as Anything
is a class member.

The class contains 4 methods (2 constructors, Finalize and ToString, two
properties).
What does the Me reference in the output statement exactly all invoke?
1. Does it call all methods but does only the ToString method lead to
printed text since it generates a string representation?
2. The Me reference cannot invoke other methods than the ToString method?
(Does not sound likely!)

Thanks for your explanation,

John

*******

ME is a reference to the current object instance. ToString, unless
overridden by the class, returns the fully qualifed class name. Most
classes override this in order to return something more meaningful.

Console.WriteLine expects a string to fill in for the {0}, so I guess
VB is nice enough to use the ToString method, or perhaps the class has
ToString as the default property. I don't like using default
properties though; it just isn't that much work to specify the
property that you want to use and it removes the chance of programmer
confusion.

Would this example work if Option Strict was on?
 
B

Bob Powell [MVP]

The Me reference is for the specific instance of a class. Me.MyMethod will
call MyMethod on the current instance. SomeOtherRef.MyMethod wil call
MyMethod on an instance other than the current one.

Me alone is just a reference to the instance. Using Me without any other
qulification such as:

Dim o as object = CType(Object,Me)

does not call any methods on the Me instance.

Many methods that require a string in .NET or need to represent an object as
something intelligible will internally call the object.ToString method to
obtain a textual representation of the object. This is what you're seeing
and this is what has confused you.

For example the WriteLine method internally does this:

Public Overridable Sub WriteLine(ByVal value As Object)
If (value Is Nothing) Then
Me.WriteLine
Else
Dim formattable1 As IFormattable = TryCast(value,IFormattable)
If (Not formattable1 Is Nothing) Then
Me.WriteLine(formattable1.ToString(Nothing,
Me.FormatProvider))
Else
Me.WriteLine(value.ToString)
End If
End If
End Sub

You can see it has nothing to do with some magical Me behaviour.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





John said:
Thanks TrtnJohn,
Yes, I understand there are 8 methods defind by class object (and that
method ToString is one of them) and that every derived class inherits
these 8
methods.

I still don't quite understand how the Me reference works. What is the
difference between Me.methodName and only Me? Does Me.methodName only call
the method methodName and does Me (alone) invoke only (but any of) the 8
methods of class object if they are listed in class CPoint3?
What if there are more methods of class object present in class CPoint3,
are
all of them executed?

Thanks for your help,

Regards,

John


TrtnJohn said:
All objects in .NET derive from the .NET base class "Object". Object
implements the ToString method and , in your case, the CPoint3 class
overrides the base class's implementation. Many Windows functions allow
you
to pass any object into it. (Including a string). When you pass an
object,
the Window's function will typically use the ToString method to display
the
actual data that represents the object. (Since all objects have this
method
because they derive from the .NET base class "Object").

John said:
Hi,
Maybe someone can explain the use of "Me" to me?
The example class CPoint3 uses the Me reference in the output
statement:
Console.WriteLine("CPoint3 constructor: {0}", Me)

The text states: "This implicitely invokes the class's ToString
method".
This would be clear if Me.ToString was used, which I tested and works
too!.
Actually, Me.Anything works and gives the same results as long as
Anything
is a class member.

The class contains 4 methods (2 constructors, Finalize and ToString,
two
properties).
What does the Me reference in the output statement exactly all invoke?
1. Does it call all methods but does only the ToString method lead to
printed text since it generates a string representation?
2. The Me reference cannot invoke other methods than the ToString
method?
(Does not sound likely!)

Thanks for your explanation,

John

*******
' Fig. 9.16: Point3.vb
' CPoint3 class represents an x-y coordinate pair.

Public Class CPoint3

' point coordinate
Private mX, mY As Integer

' default constructor
Public Sub New()

' implicit call to Object constructor occurs here
X = 0
Y = 0
Console.WriteLine("CPoint3 constructor: {0}", Me)
End Sub ' New

' constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer)

' implicit call to Object constructor occurs here
X = xValue
Y = yValue
Console.WriteLine("CPoint3 constructor: {0}", Me)
End Sub ' New

' finalizer overrides version in class Object
Protected Overrides Sub Finalize()
Console.WriteLine("CPoint3 Finalizer: {0}", Me)
MyBase.Finalize() ' call Object finalizer
End Sub ' Finalize

' property X
Public Property X() As Integer

Get
Return mX
End Get

Set(ByVal xValue As Integer)
mX = xValue ' no need for validation
End Set

End Property ' X

' property Y
Public Property Y() As Integer

Get
Return mY
End Get

Set(ByVal yValue As Integer)
mY = yValue ' no need for validation
End Set

End Property ' Y

' return String representation of CPoint3
Public Overrides Function ToString() As String
Return "[" & mX & ", " & mY & "]"
End Function ' ToString

End Class ' CPoint3
 
C

Cor Ligthert [MVP]

John,

Did you ever saw in JavaScript or any other C derived language the use of
"This"?
Me is the same in VB.

So if you have a form where you have overloaded the sub New with by instance

Public sub New(byval This as form)
etc.

Than you can do this
dim frm as new formX(me)

You give than the complete reference from your currenct form to FormX which
is in that than This (If you place that first global to use it outside the
sub new).

I hope that this gives an idea

Cor
 
G

Guest

Bob, TrtnJohn, dgk, Cor,

Thanks for your responses. Good to see what happens behind the Writeline
method, but some aspects are yet unfamiliar to me (like "IFormattable" and
"TryCast") and we probably should not dive into those before I truely
understand the "Me" reference.
I have no experience in C derived languages and have not yet encountered
"This".

I do understand how "Me.MethodName" works, it will execute the method called
"MethodName" in the current instance. I need to understand what happens when
only "Me" is used. It looks like when only "Me" is used, that the program
starts looking in the current instance for any methods that are defined under
class Object (of which ToString is one) and executes those found. Is that
what happens?
If that is the case, what happens if there are more methods in the current
instance which are also defined under class Object. Are the all executed? In
that case using "Me.MethodName" instead of "Me" would prevent execution of
methods in the current instance which should not be executed at that spot.

Thanks again for your help?

John


Bob Powell said:
The Me reference is for the specific instance of a class. Me.MyMethod will
call MyMethod on the current instance. SomeOtherRef.MyMethod wil call
MyMethod on an instance other than the current one.

Me alone is just a reference to the instance. Using Me without any other
qulification such as:

Dim o as object = CType(Object,Me)

does not call any methods on the Me instance.

Many methods that require a string in .NET or need to represent an object as
something intelligible will internally call the object.ToString method to
obtain a textual representation of the object. This is what you're seeing
and this is what has confused you.

For example the WriteLine method internally does this:

Public Overridable Sub WriteLine(ByVal value As Object)
If (value Is Nothing) Then
Me.WriteLine
Else
Dim formattable1 As IFormattable = TryCast(value,IFormattable)
If (Not formattable1 Is Nothing) Then
Me.WriteLine(formattable1.ToString(Nothing,
Me.FormatProvider))
Else
Me.WriteLine(value.ToString)
End If
End If
End Sub

You can see it has nothing to do with some magical Me behaviour.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





John said:
Thanks TrtnJohn,
Yes, I understand there are 8 methods defind by class object (and that
method ToString is one of them) and that every derived class inherits
these 8
methods.

I still don't quite understand how the Me reference works. What is the
difference between Me.methodName and only Me? Does Me.methodName only call
the method methodName and does Me (alone) invoke only (but any of) the 8
methods of class object if they are listed in class CPoint3?
What if there are more methods of class object present in class CPoint3,
are
all of them executed?

Thanks for your help,

Regards,

John


TrtnJohn said:
All objects in .NET derive from the .NET base class "Object". Object
implements the ToString method and , in your case, the CPoint3 class
overrides the base class's implementation. Many Windows functions allow
you
to pass any object into it. (Including a string). When you pass an
object,
the Window's function will typically use the ToString method to display
the
actual data that represents the object. (Since all objects have this
method
because they derive from the .NET base class "Object").

:

Hi,
Maybe someone can explain the use of "Me" to me?
The example class CPoint3 uses the Me reference in the output
statement:
Console.WriteLine("CPoint3 constructor: {0}", Me)

The text states: "This implicitely invokes the class's ToString
method".
This would be clear if Me.ToString was used, which I tested and works
too!.
Actually, Me.Anything works and gives the same results as long as
Anything
is a class member.

The class contains 4 methods (2 constructors, Finalize and ToString,
two
properties).
What does the Me reference in the output statement exactly all invoke?
1. Does it call all methods but does only the ToString method lead to
printed text since it generates a string representation?
2. The Me reference cannot invoke other methods than the ToString
method?
(Does not sound likely!)

Thanks for your explanation,

John

*******
' Fig. 9.16: Point3.vb
' CPoint3 class represents an x-y coordinate pair.

Public Class CPoint3

' point coordinate
Private mX, mY As Integer

' default constructor
Public Sub New()

' implicit call to Object constructor occurs here
X = 0
Y = 0
Console.WriteLine("CPoint3 constructor: {0}", Me)
End Sub ' New

' constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer)

' implicit call to Object constructor occurs here
X = xValue
Y = yValue
Console.WriteLine("CPoint3 constructor: {0}", Me)
End Sub ' New

' finalizer overrides version in class Object
Protected Overrides Sub Finalize()
Console.WriteLine("CPoint3 Finalizer: {0}", Me)
MyBase.Finalize() ' call Object finalizer
End Sub ' Finalize

' property X
Public Property X() As Integer

Get
Return mX
End Get

Set(ByVal xValue As Integer)
mX = xValue ' no need for validation
End Set

End Property ' X

' property Y
Public Property Y() As Integer

Get
Return mY
End Get

Set(ByVal yValue As Integer)
mY = yValue ' no need for validation
End Set

End Property ' Y

' return String representation of CPoint3
Public Overrides Function ToString() As String
Return "[" & mX & ", " & mY & "]"
End Function ' ToString

End Class ' CPoint3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top