0

I was asked, what I thought was a simple question, until I thought about it.

"Is there ever a need for using Me. (VB.Net) or this. (C#) - when referring to properties belonging to the same scope?"

i.e. There are some legacy projects we work on and a co-worker was using class properties via Me.[Property] - I noticed this and explained that he can simply access the property directly (remove the Me.).

(VB.Net)
Public Property Name() As String
Me.Name = "My Name" vs. Name = "My Name"

(C#)
public string Name{ get; set; }
this.Name = "My Name"; vs. Name = "My Name";

I was wondering though, is there a reason (other than passing the entire class / object, which I believe is a different topic) that you would ever need to call Me. or this.

Or is it simply legacy code left in place that causes no harm (has no positive or negative effect)?

Hexie
  • 3,955
  • 6
  • 32
  • 55
  • You should, den only it will refer current properties & class.... – Prasad Raja May 03 '16 at 05:19
  • @Ian Mercer - I don't believe this is a duplicate, please re-consider what the question asks. Also - the "answered" question referred only to C# - is it assumed its the same for VB (Me.)? – Hexie May 03 '16 at 05:23
  • 1
    This and me is a matter of code style. Some prefer implicit and others explicit. The only time it matters is when there is ambiguity, which also only happens because of code style. – Andrew Tavera May 03 '16 at 05:24
  • 2
    @Hexie I believe it's still a duplicate. `Me.` and `this.` are exactly the same (and compile to the same code). There are a handful of cases where you *must* use `this.` which are covered in the linked question - otherwise it is purely coding style, and it has no effect whatsoever – Rob May 03 '16 at 05:35
  • 1
    It is just a compiler safe way of mimicking conventions. Some will use s_someStaticField and m_someMemberField and have no prefix for local variables. An alternative is using ClassName.someStaticField, this.someMemberField and someLocalVariable with no qualifier. The only time it means something to the compiler is when you have for example a local variable with the same name as a class level field. Then you would need to use the this qualifier to tell the compiler that you mean the class level field instead of the local variable. – Andrew Tavera May 03 '16 at 05:44
  • It makes using VS intellisense easier to use by quickly getting a list of the current instance members. – Dave Doknjas May 03 '16 at 14:04

0 Answers0