none
What does "Me" mean in Visual Basic? RRS feed

Answers

  • It means the current object instance.

    --
    Mike
    • Proposed as answer by Cor Ligthert Saturday, September 18, 2010 8:24 AM
    • Marked as answer by Chao KuoModerator Friday, September 24, 2010 11:37 AM
    Saturday, September 18, 2010 2:36 AM
  • Hi,

    Another way of thinking of ME is that you would use ME to refer to yourself.

    Within a section of code, within a Class, ME refers to itself as shown below.

    ( The equivalent in Visual C# and Visual C++ I believe is the keyword "this " )

     

    So to refer the code to point to itself such as a PRIVATE value within a class, you can precede code with Me.

    You can then quickly see the PRIVATE values and PROPERTIES of

    the code you create especially when you write any CLASS code.

    '-----------------------------------------------------------------------------------------------------------

     

     

    Public Class Form1

    Public Sub ExampleMethod()

    Me .Text = “Hello World!!”

    End Sub

    End Class

     

    ' In the above Form class code Me refers to Form1

     

    ' In the code below Me refers to ExampleClass

     

    Public Class ExampleClass

    Private name As String

    Public Sub New(ByVal aNameString As String)

    Me .name = aNameString

    End Sub

    End Class

     

     

    Hope this helps and paints a clearer picture for you.    :-)    ;-)    :-D

     



    Regards,

    John


    Saturday, September 18, 2010 8:32 PM

  • Usually using the Me keyword is not necessary.  Here is a contrived example of when it would be necessary:
     
    public class Demo
        public Name as String
       
        public sub SetTheName(Name as string)
            Me.Name = name
        end sub
    end class
     
    In the case above, you would need to use the keyword Me, to differentiate between the parameter to the subroutine named Name, and the field in the class with the same name.
     
    As I said, the example is contrived, as you should not name the parameter the same as the field.

    --
    Mike
    • Proposed as answer by Cor Ligthert Saturday, September 18, 2010 8:25 AM
    • Marked as answer by Chao KuoModerator Friday, September 24, 2010 11:37 AM
    Saturday, September 18, 2010 3:02 AM

All replies

  • It means the current object instance.

    --
    Mike
    • Proposed as answer by Cor Ligthert Saturday, September 18, 2010 8:24 AM
    • Marked as answer by Chao KuoModerator Friday, September 24, 2010 11:37 AM
    Saturday, September 18, 2010 2:36 AM
  • When would I use it exactly?

    Saturday, September 18, 2010 2:54 AM
  • To add to what Mike said ... for example:

    If you are on a form, Me.Text sets the title text of the form. Me.Close closes the form and so on.

    Hope this helps. 


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Saturday, September 18, 2010 3:00 AM

  • Usually using the Me keyword is not necessary.  Here is a contrived example of when it would be necessary:
     
    public class Demo
        public Name as String
       
        public sub SetTheName(Name as string)
            Me.Name = name
        end sub
    end class
     
    In the case above, you would need to use the keyword Me, to differentiate between the parameter to the subroutine named Name, and the field in the class with the same name.
     
    As I said, the example is contrived, as you should not name the parameter the same as the field.

    --
    Mike
    • Proposed as answer by Cor Ligthert Saturday, September 18, 2010 8:25 AM
    • Marked as answer by Chao KuoModerator Friday, September 24, 2010 11:37 AM
    Saturday, September 18, 2010 3:02 AM

  • Usually using the Me keyword is not necessary.  Here is a contrived example of when it would be necessary:
     
    public class Demo
        public Name as String
       
        public sub SetTheName(Name as string)
            Me.Name = name
        end sub
    end class
     
    In the case above, you would need to use the keyword Me, to differentiate between the parameter to the subroutine named Name, and the field in the class with the same name.
     
    As I said, the example is contrived, as you should not name the parameter the same as the field.

    --
    Mike
    In my idea the exact answer beside that some use it to start the intellisence 
    Success
    Cor
    Saturday, September 18, 2010 8:29 AM
  • Very good point, Cor!  In fact, I do that occasionally, but then I delete the "Me.".

    --
    Mike
    Saturday, September 18, 2010 1:00 PM
  • Hi,

    Another way of thinking of ME is that you would use ME to refer to yourself.

    Within a section of code, within a Class, ME refers to itself as shown below.

    ( The equivalent in Visual C# and Visual C++ I believe is the keyword "this " )

     

    So to refer the code to point to itself such as a PRIVATE value within a class, you can precede code with Me.

    You can then quickly see the PRIVATE values and PROPERTIES of

    the code you create especially when you write any CLASS code.

    '-----------------------------------------------------------------------------------------------------------

     

     

    Public Class Form1

    Public Sub ExampleMethod()

    Me .Text = “Hello World!!”

    End Sub

    End Class

     

    ' In the above Form class code Me refers to Form1

     

    ' In the code below Me refers to ExampleClass

     

    Public Class ExampleClass

    Private name As String

    Public Sub New(ByVal aNameString As String)

    Me .name = aNameString

    End Sub

    End Class

     

     

    Hope this helps and paints a clearer picture for you.    :-)    ;-)    :-D

     



    Regards,

    John


    Saturday, September 18, 2010 8:32 PM
  • Direct, to the point, with an easy to understand example. How refreshing is that?

    Thank you.


    BiggyD

    Monday, June 25, 2018 5:22 PM