tags:

views:

1440

answers:

6

I'm currently working on a web application (ASP.NET) and some of the pages that are included were created by other programmers, what I noticed is, they are not using "Me(VB.NET)" keyword to access controls, while on my side I used it in every page that I've created. Just to give further information, the web application runs on a .NET Framework 2.0. Does anyone out there who will help me understand is it required to use Me or not? What are the advantages and disadvantages in the whole code if you're using Me? Can it improve the performance of the application?

Any help is highly appreciated. Thanks in advance.

+9  A: 

From what I understand, it is purely preference, but can disambiguate between properties and locally scoped variables/parameters. Some people include it religiously out of habit. This way if they were to then introduce a local variable of the same name, nothing would break.

Say you have a method defined DoSomething(MyString As String) and the class also has a String called MyString. The only way to reference the class version of MyString is to use Me.MyString. Thus, you say Me.MyString = MyString and the compiler knows what you want.

lc
+1  A: 

I think for UI controls its very much user's preference.

But I strongly prefer use of "Me" especially if you are using class properties just in case there is some code that needs to be executed before you access that variable.

Example

Class Sample
       Private myString as String

       Public Property GetMyString As String
       Get
          ' Some code here which sets the string after reading value from the  database
       End Get
       Set
       End Set

       End Property        
End Class

In this case there is a difference between using it with or without "Me" keyword.

MOZILLA
that code would not work in vb.net as it is case insensitive unlike c#.
Tom Anderson
I have corrected that now.
MOZILLA
thanks for the update :)
Tom Anderson
+1  A: 

The Me keyword is optional in the same way you have the option to fully qualify a namespace even though you have imported it at the top of the class.

However if you have a control on the form with the same name as another object in another imported namespace it may be essential.

John
+5  A: 

In some cases it is required, let's take for example a variable in the scope of a class, and a parameter being passed to a sub-routine that has an equivalent name like so:

Private theString As String

Private Sub Setup(ByVal theString As String)
   Me.theString = theString
End Sub

It brings things into the correct context to allow assignments to be carried out correctly.

In your case some developers just tend to do it so they know where the variable is coming from, something my work mates will do also. It's also commonly done because intellisense will bring up their local variable list if they type in "Me.", something I don't usually do, although I probably should do more.

Kezzer
+1  A: 

Using the Me keyword doesn't affect performance. It is purely a matter of style. I think it improves clarity to your code if you use it. So you know at first glance that the member used belongs to the object itself and it is not a local variable for example.

The Me keyword is used any time we want our code to refer to members (methods, properties, ...) within the current object. The Me keyword is analogous to the this keyword in C++ and C# languages. The Me keyword is usually optional, since any method call is assumed to refer to the current object unless explicitly noted otherwise.

The exception is when you're using shadowed variables. A shadowed variable is a variable at procedure level with the same name as a variable of the class. For example:

Public Class ExampleClass

  Private FirstName As String

  Public Sub ExampleMethod()
    Dim FirstName As String
    FirstName = "Stefan"
  End Sub

End Class

The variable FirstName is declared at the class level and within the ExampleMethod method. Within ExampleMethod only the local, or shadowed, variable is used unless we explicitly reference the class-level variable with the Me keyword:

Public Class ExampleClass

  Private FirstName As String

  Public Sub ExampleMethod()
    Dim FirstName As String
    FirstName = "Stefan"
    Me.FirstName = "Carl Gustav"
  End Sub

End Class
splattne
A: 

Me is a object of current class

Dhiraj