473,569 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB-101 "Me"

Hi,
Maybe someone can explain the use of "Me" to me?
The example class CPoint3 uses the Me reference in the output statement:
Console.WriteLi ne("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.WriteLi ne("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.WriteLi ne("CPoint3 constructor: {0}", Me)
End Sub ' New

' finalizer overrides version in class Object
Protected Overrides Sub Finalize()
Console.WriteLi ne("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

Nov 21 '05 #1
6 1658
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" wrote:
Hi,
Maybe someone can explain the use of "Me" to me?
The example class CPoint3 uses the Me reference in the output statement:
Console.WriteLi ne("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.WriteLi ne("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.WriteLi ne("CPoint3 constructor: {0}", Me)
End Sub ' New

' finalizer overrides version in class Object
Protected Overrides Sub Finalize()
Console.WriteLi ne("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

Nov 21 '05 #2
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" wrote:
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" wrote:
Hi,
Maybe someone can explain the use of "Me" to me?
The example class CPoint3 uses the Me reference in the output statement:
Console.WriteLi ne("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.WriteLi ne("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.WriteLi ne("CPoint3 constructor: {0}", Me)
End Sub ' New

' finalizer overrides version in class Object
Protected Overrides Sub Finalize()
Console.WriteLi ne("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

Nov 21 '05 #3
dgk
On Tue, 25 Oct 2005 10:34:03 -0700, "John"
<Jo**@discussio ns.microsoft.co m> wrote:
Hi,
Maybe someone can explain the use of "Me" to me?
The example class CPoint3 uses the Me reference in the output statement:
Console.WriteL ine("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.WriteLi ne 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?
Nov 21 '05 #4
The Me reference is for the specific instance of a class. Me.MyMethod will
call MyMethod on the current instance. SomeOtherRef.My Method 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,I Formattable)
If (Not formattable1 Is Nothing) Then
Me.WriteLine(fo rmattable1.ToSt ring(Nothing,
Me.FormatProvid er))
Else
Me.WriteLine(va lue.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" <Jo**@discussio ns.microsoft.co m> wrote in message
news:4D******** *************** ***********@mic rosoft.com...
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" wrote:
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" wrote:
> Hi,
> Maybe someone can explain the use of "Me" to me?
> The example class CPoint3 uses the Me reference in the output
> statement:
> Console.WriteLi ne("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.WriteLi ne("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.WriteLi ne("CPoint3 constructor: {0}", Me)
> End Sub ' New
>
> ' finalizer overrides version in class Object
> Protected Overrides Sub Finalize()
> Console.WriteLi ne("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
>
>
>

Nov 21 '05 #5
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
Nov 21 '05 #6
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 "IFormattab le" 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.MethodN ame" 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.MethodN ame" 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 [MVP]" wrote:
The Me reference is for the specific instance of a class. Me.MyMethod will
call MyMethod on the current instance. SomeOtherRef.My Method 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,I Formattable)
If (Not formattable1 Is Nothing) Then
Me.WriteLine(fo rmattable1.ToSt ring(Nothing,
Me.FormatProvid er))
Else
Me.WriteLine(va lue.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" <Jo**@discussio ns.microsoft.co m> wrote in message
news:4D******** *************** ***********@mic rosoft.com...
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" wrote:
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" wrote:

> Hi,
> Maybe someone can explain the use of "Me" to me?
> The example class CPoint3 uses the Me reference in the output
> statement:
> Console.WriteLi ne("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.WriteLi ne("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.WriteLi ne("CPoint3 constructor: {0}", Me)
> End Sub ' New
>
> ' finalizer overrides version in class Object
> Protected Overrides Sub Finalize()
> Console.WriteLi ne("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
>
>
>


Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.