Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Me (VB.NET) keyword is it required or not?

Tags:

asp.net

vb.net

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.

like image 318
KG Sosa Avatar asked Jan 09 '09 07:01

KG Sosa


People also ask

What is the use of me keyword?

The keyword Me refers to the current instance of an object. The Me reference is a hidden reference to every non-shared method of a class; shared methods are discussed later in this chapter. Each method can refer to the other methods and variables of that object by way of the Me reference.

Is VB.NET still useful?

Is VB.NET still useful today? - Quora. Yes, it is useful. It is not about the language, is about your ability to solve a problem and be able to translate that solution into a thing that the computer understand. All the languages have advantages and disadvantages, some are better in some circumstances other in others.


2 Answers

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.

like image 104
lc. Avatar answered Oct 12 '22 13:10

lc.


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.

like image 25
Kezzer Avatar answered Oct 12 '22 11:10

Kezzer