Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why is "this.SomeVar" used in C# wont "SomeVar" work?

Why is "this.SomeVar" used in C# wont "SomeVar" work?

Scheduled Pinned Locked Moved C#
questioncsharpc++
3 Posts 3 Posters 17 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I notice in a lot of C# code that "this.SomeVar" is used when the "SomeVar" could be called directly. I removed the "this." from a WebApplication and it seemed to work OK :) What is it doing and why is it here? Is it the same as the C++ "this->"? Thanks Note I only used C# for a day so if this is a stupid question I am sorry.:confused:

    R V 2 Replies Last reply
    0
    • L Lost User

      I notice in a lot of C# code that "this.SomeVar" is used when the "SomeVar" could be called directly. I removed the "this." from a WebApplication and it seemed to work OK :) What is it doing and why is it here? Is it the same as the C++ "this->"? Thanks Note I only used C# for a day so if this is a stupid question I am sorry.:confused:

      R Offline
      R Offline
      Russell Morris
      wrote on last edited by
      #2

      My code looks like this (before cleaning, anyway) because I usually manage to forget the names of member variables. Typing "this." brings up a list of member variables, letting me search for my member variable's name without having to change my place in the code. So, short answer - laziness - at least in my case :) -- Russell Morris Georgia Institute of Technology "Lisa, just because I don't care doesn't mean I'm not listening..." - Homer

      1 Reply Last reply
      0
      • L Lost User

        I notice in a lot of C# code that "this.SomeVar" is used when the "SomeVar" could be called directly. I removed the "this." from a WebApplication and it seemed to work OK :) What is it doing and why is it here? Is it the same as the C++ "this->"? Thanks Note I only used C# for a day so if this is a stupid question I am sorry.:confused:

        V Offline
        V Offline
        V 1
        wrote on last edited by
        #3

        Hi! The [this] keyword is used so that you can refer to a member variable instance without accidently calling another variable by the same name. Take this example: class MyExample { private string value = ""; public string Text { get { return this.value; } set { this.value = value; } } } Now personally I would not have written code like this, but there could be a time when something similar occurs. In this example, value is the variable name sent in though the Text Property and can't be renamed. Since I have an internat member called value, the only way that I can communicate with it is with the [this] call, which tells the compiler you were talking about the global instance and not the system local instance. Using the [this] statement is not required by any means, but can help tell other developers that you are: A. Using a member property and B. There can not be a conflict in local naming. I hope this helps. ;P -V-

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups