The Me keyword is used in. NET (Turn)

Source: Internet
Author: User
Tags integer reference
The keyword Me keyword is used in. Net
Www.yescnet.com CNET Chinese web

The ME function is familiar to everyone, and in VB6 I can only apply to forms, but in. NET does not use this limit, quick to see how to do it!!!!!!!!

?????? Content??????
V 1. Utilize me reference value
1.1 Know me reference value
1.2 Program returns ME reference value

V 2. Get a deep understanding of me reference values

1. Use Me reference value
1.1 Know me reference value

Class Program members (Procedure member) each contain a me reference variable, which is always referenced to the current object. The object at present is the object that is receiving and processing the message. e.g.

' Ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'------------------------------------------------------------------------------
Public Class Fee
Private Amount as Decimal

Public Sub New (ByVal amt as Decimal)
Me.amount = Amt
End Sub
Public Sub disp ()
MessageBox.Show ("Amount is" + str (me.amount))
End Sub
End Class
'--------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
.......
#End Region
Protected Sub Form1_Click (ByVal sender as Object,
ByVal e as System.EventArgs)
Dim A as New Fee (100)
Dim B as New Fee (80)
A.disp ()
B.disp ()
End Sub
End Class

This program outputs the following:amount is 100
Amount is 80

A and B are the objects of the fee category. When the computer executes the instructions--
A.disp ()

A is that the me in the current object,disp () program is referring to object A.

can also be seen as:

Figure 1, me reference value and current object

Note that the:me reference to the object a, that is, me and a refer to the same object.
When the computer executes another instruction ──b.disp (), B is the current object, and the disp () program's me reference is pointing to object B.

Because me is referencing to the object b, so me and B refer to the same object. When writing a program, it is advisable to make full use of me reference.

1.2 Program returns ME reference value

In application, the program often returns me reference value, can create wonderful effect, this effect is also an important feature of VB. I hope you can carefully understand the use of the Me pointer, so that you can write the perfect OOP program! Now, take a look at a familiar program

' Ex02.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'------------------------------------------------------------------------------
Public Class Money
Private Balance as Decimal
Public Sub New (ByVal amount as Decimal)
Balance = Amount
End Sub
Public Sub Add (ByVal saving as Decimal)
Balance = balance + saving
End Sub
Public Sub Display ()
MessageBox.Show ("Balance is" + str (Balance))
End Sub
End Class
'--------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
.......
#End Region
Protected Sub Form1_Click (ByVal sender as Object,
ByVal e as System.EventArgs)
Dim Orange as New money (100)
Orange.add (300)
Orange.add (80)
Orange. Display ()
End Sub
End Class

This program outputs the following:balance is 480

Money category of balance information, record the balance of deposits. The object in main () Orange accepts two messages ──add (300) and add (80) to deposit two amounts.

Directive ──orange.add (300)
Orange.add (80)

said: First deposit 300 yuan and deposit 80 yuan, there are priorities. If the above pattern is changed to-

is more of a sense of order. So the instructions are equivalent to--

This effect, not too unfamiliar! Remember the primary school, the monitor shouted: "Stand up, salute, sit down," you do not receive three consecutive messages? Gradually, you have been able to design as a kind of daily life of the object. However, as the saying goes,:"High building ground", or must first have a good understanding of the me reference to the line! Please look at a program

' Ex03.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'-----------------------------------------------------------------------------------
Public Class Money
Private Balance as Decimal
Public Sub New (ByVal amount as Decimal)
Balance = Amount
End Sub
Public Function Add (ByVal saving as Decimal) as
Balance = balance + saving
Add = Me
End Function
Public Sub Display ()
MessageBox.Show ("Balance is" + str (Balance))
End Sub
End Class
'----------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
.......
#End Region
Protected Sub Form1_Click (ByVal sender as Object,
ByVal e as System.EventArgs)
Dim Orange as New money (100)
Orange.add. Add (80)
Orange. Display ()
End Sub
End Class

This program outputs the following:balance is 480
Since I always refer to the current object, I am now referring to the object orange.

Figure 2, the reference value that the program returns to the current object

The Oragne object is the object of me, and it can be said that me and orange refer to the same object. Directive--
Add = Me

Returns the reference value of the current object-that is, the Orange object. The definition of the Add () program-

So add () returns the reference value of the current object to Form1_Click (). At the moment the value of,orange.add (300) is also a reference value, and Orange is referenced to the same object.

So, the Form1_Click () program's instructions--

Become the alias of the Orange object.
The original instruction ──orange.add. Add (80)
Equivalent to ──orange.add (80)

However, at this time the Orange object of the balance variable value of 400 yuan, rather than the original 100 yuan. This orange receives the message ──add (80) and its balance value increases to 480 yuan. When Orange receives the 2nd message ──add (80), the computer then executes the Add () program, which returns the reference value of orange again, making the entire instruction-

and become the alias of Orange. Therefore, the DISP () message can be followed up as follows:

' Ex04.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'------------------------------------------------------------------------------------
Public Class Money
Private Balance as Decimal
Public Sub New (ByVal amount as Decimal)
Balance = Amount
End Sub
Public Function Add (ByVal saving as Decimal) as
Balance = balance + saving
Add = Me
End Function
Public Sub Display ()
MessageBox.Show ("Balance is" + str (Balance))
End Sub
End Class
'-------------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
.......
#End Region
Protected Sub Form1_Click (ByVal sender as Object,
ByVal e as System.EventArgs)
Dim Orange as New money (100)
Orange.add. Add (80). Display ()
End Sub
End Class

This program outputs the following:balance is 480
The Orange object receives the 1th message ──add (300), the computer executes the Add () program, executes to the end instruction, returns the reference value of me (that is, the Orange object). At this time, the Orange.add (300) of Form1_Click () is the reference value of the Orange object, i.e. Orange.add () is the alias of the Orange object; then orange and Orange.add (300) coincide together, Represents the same object--the original orange object.

Next, the 2nd message, ──add (80), is passed to Orange.add (300), which is equivalent to an orange object. Once again, the add = Me instruction in Add () makes Orange.add. Add (80) the alias of Orange.add (300), the alias of Orange; then the three represent the same object-the original orange object.

Next, the 3rd message ──display to Orange.add. Add (80), which is the equivalent of passing to the Orange object.

The balance value within the Orange object is then output.
The technique of returning me reference values by program will be applied in many ways. To get a better understanding of this approach, see a special case where the program returns the reference value of the new object. This object is not the current object, but the content is copied from the current object. This is different from returning me reference values, and the two usages are often confusing! Now, change the procedure to--

' Ex05.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'------------------------------------------------------------------------------------
Public Class Money
Private Balance as Decimal
Public Sub New (ByVal amount as Decimal)
Balance = Amount
End Sub
Public Function Add (ByVal saving as Decimal) as
Dim newobj as Money
newobj = New Money (balance + saving)
Add = newobj
End Function
Public Sub Display ()
MessageBox.Show ("Balance is" + str (Balance))
End Sub
End Class
'-------------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
.......
#End Region
Protected Sub Form1_Click (ByVal sender as Object,
ByVal e as System.EventArgs)
Dim Orange as New money (100)
Orange.add. Add (80). Display ()
End Sub
End Class

This program outputs the following:balance is 480

When the Orange object receives the 1th message ──add (300), the computer executes the Add () program, giving birth to a new object of the money category, which copies the current object content (that is, the value of the Orange object) to Form1_Click (). This copy is the value of Orange.add (300).

Orange.add (300) is the copy returned to the object, not the original orange object. When the message ──add (80) is passed to the object represented by Orange.add (300), the computer executes the Add () function, at which point the current object is Orange.add (300) rather than the original orange. At the time of execution, a copy of the current object ──orange.add (300) is copied to the new-born object and returned to the Form1_Click () program, which is the value of Orange.add. Add (80).

Because each time you execute add () produces a new object (although the content is the same, but it occupies a different memory space), the subsequent message is passed to the new object created by Add (), not to the Orange object, so none of these messages affect the original Orange object's content.
Please note that:display () does not return the reference value of the object, then the Display () message must not be followed by another message. So if the Form1_Click () program is rewritten as follows, it's wrong--

Protected Sub Form1_Click (ByVal sender as Object,
ByVal e as System.EventArgs)
Dim Orange as New money (100)
Orange.add (300). Display (). Add () ' error!
End Sub
End Class

Because display () does not return the reference value of the object, the instruction

The subsequent message ──add (80) was wrong. How to correct it? Very simply, simply call the display () program to return Me (the reference value of the current object) or the reference value of the new object, as follows:

' Ex06.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'-------------------------------------------------------------------------------------
Public Class Money
Private Balance as Decimal
Public Sub New (ByVal amount as Decimal)
Balance = Amount
End Sub
Public Function Add (ByVal saving as Decimal) as
Dim newobj as Money
newobj = New Money (balance + saving)
Add = newobj
End Function
Public Function Display () as Money
MessageBox.Show ("Balance is" + str (Balance))
Display = Me
End Function
End Class
'-----------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
........
#End Region
Protected Sub Form1_Click (ByVal sender as Object,
ByVal e as System.EventArgs)
Dim Orange as New money (100)
Orange. Display (). Add (300). Display (). Add (80). Display ()
End Sub
End Class

This program outputs:
Balance is 100
Balance is 400
Balance is 480

In this procedure, the,orange accepts display () message, prints out the deposit amount, accepts Add (300) message, increases the deposit amount by 300 yuan, and then accepts display () message. Display () returns the current object orange reference value, add () is returned to the new birth object's reference value.

2. Deep understanding of me reference

When VB compiles, it automatically generates me reference variables for program members and makes me fix references to current objects. In this will discuss how VB generates me reference variables, so that you can more deeply understand the ME reference variable characteristics and role. First of all, when compiling a program member, the,VB will secretly add 1 reference parameters ──me, become the 1th parameter of the program member. e.g.

Class person
Private name as String
Priavte Age as Integer
Public Sun New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
End Sub
Public Sub Display ()
MessageBox.Show (name + "," + str (age))
End Sub
End Class

When compiling this program,vb will steal plus me reference variables as follows:

Class person
Private name as String
Priavte Age as Integer
Public Sun New (ByVal Me as person, ByVal na as String,
ByVal A as Integer)
Me.Name = NA
Me.age = A
End Sub
Public Sub Display (ByVal Me as Person)
MessageBox.Show (Me.Name + "," + str (me.age))
End Sub
End Class

You cannot change the value of me, either in the new () or display () program, where the,me is fixed to the current object. Next, see How to make me refer to the current object? If there is a form1_click () procedure as follows:

Sub Form1_Click (...)
Dim x as New person ("Tom", 26)
X.display ()
End Sub

VB in compiling, will be the instructions ──x. Display () is converted to:
Person_display (x)

Means: Call the display () program of the person category to handle the contents of the X object. When you call this program, you pass the X reference value to the Me parameter in the display () program, as follows:

So,me is fixed on the X object, and this X object is what we want to deal with, and
That is the current object. Please look at another example. If the person category is changed to:

Class person
Private name as String
Priavte Age as Integer
Public Sun New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
End Sub
Public Sub Print ()
Me.display ()
End Sub
Public Sub Display ()
MessageBox.Show (name + "," + str (age))
End Sub
End Class

Then VB will add me reference value for these 3 program members. where the print () program is converted as follows:

Public Sub Print (ByVal Me as Person)
Person_display (Me)
End Sub

After converting the above Me.display () to Person_display (me), you pass the Me value in this print () to the Me in Display (), at which point I refer to the current object for all two programs.
The general program members mentioned above do not include shared program members (shared member Function). You remember that? The purpose of a shared program member is to handle things about the entire category, not the content of the object. On the other hand,me reference to the current object, the general program member through me to access the content of the current object. Since the shared program members do not have to access the value of a particular object, of course, I do not need to reference variables, so VB does not for the shared program members to steal plus me reference variables. In short, the,VB rule is--

When "vb compile, you do not add me reference variables for shared program members, so
The shared program member has no me reference variable and cannot access the object's contents.

As mentioned earlier, when calling a generic program member, you must pass the reference value of the current object to the program member. However, there is no me reference in the shared program member, so it is not possible to call general program members. Can learn the rules of VB--
"Shared program members cannot call general program members, but can call other shared program members"

Conversely, general program members can call shared program members. e.g.

Class person
Private name as String
Priavte Age as Integer
Shared Plast as Person

Public Sun New (ByVal na as String, ByVal A as Integer)
name = NA
Age = A
Plast = Me
End Sub
Shared Sub Displast ()
Plast. Display ()
' Display () Error!!
End Sub
Public Sub Print ()
Displast ()
End Sub
Public Sub Display ()
MessageBox.Show (name + "," + str (age))
End Sub
End Class

Display () is a general program member, the shared program member Displast () can not directly call display () program as follows:
Shared Sub Displast ()
Display () Error!!
End Sub

Because VB will convert it to--

Shared Sub Displast ()
Person_display (Me) Error!!
End Sub

But Displast () is a shared program member with no me reference variable, so it's wrong. As for print ()
Call Displast (),vb convert it to--

Public Sub Print (ByVal Me as Person)
Person_displast ()
End Sub

Although there is a me reference inside print (), Displast () is a static program member and does not need me
,print () does not pass me to Displast (), which is right.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.