Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

What does `this` keyword indicates?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    What does `this` keyword indicates?

    1) i see in some indis
    Code:
    this
    keyword:

    Code:
      public string XYZ
            {
                get { return [COLOR="Red"]this[/COLOR].ABC;  }
            }
            ......
            private string ABC = ....;
    is it needed at all? what are pros and cons of using that, cant we just use ?


    2) I also see `base` keyword in different methods.. like:

    Code:
                if (State == State.SetDefaults)
                {
                    [COLOR="Red"]base[/COLOR].Calculate = Calculate.OnBarClose;
                    [COLOR="red"]base[/COLOR].IsOverlay = (true);
                    .......
                }
    or

    Code:
    (base.Close[3] > base.High[3])
    what happens with or without using that? can you tell me example of cases?
    Last edited by ttodua; 02-02-2018, 03:01 AM.

    #2
    Hello. Thank you for the post.

    The 'this' keyword means that you are self-referencing the instance of the object you are working with. Everything in C# is an object and objects have the ability to have multiple instances of themselves (as long as that object is not static).

    Please read this page for more information on the 'this' keyword:
    The this keyword is a special type of reference variable, which is implicitly defined within each constructor and non-static method as a first parameter of the type class in which it is defined.

    https://en.wikipedia.org/wiki/Instan...mputer_science)

    The 'base' keyword is like the 'this' keyword, only it is referencing the parent class that the object inherits.

    Please see these pages on inheritance and the base keyword:
    Inheritance in C# enables you to create new classes that reuse, extend, and modify the behavior defined in other classes.

    Learn about the base keyword, which is used to access members of the base class from within a derived class in C#.


    Please let us know if you have any questions.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Chris,
      In general I knew that what you are saying.
      I just want to understand, what they do in the indicators, what is the difference, i asked that.

      to see example scenarios, when they matter at all..
      in real-life examples, i dont udnersatnd how or whenever in Ninjatrader `this` or `base` might be needed?

      Comment


        #4
        Hello,

        Thank you for the reply.

        You must use these keywords to be explicit in whatever data you need to get. Using this is an unambiguous way to say "get the variable that belongs to this instance of this object specifically". Here is a publicly available post on the times you would use 'this':
        I know that this refers to a current object. But I do not know when I really need to use it. For example, will be there any difference if I use x instead of this.x in some of the methods? May be x ...


        Using 'base' is used to access methods and properties that the child class does not have. The reason it is used in your example is to access the properties of the Indicator class i.e. the parent class of your indicator.

        Please let us know if we may be of any further assistance.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by hazylizard, Today, 08:38 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Erick  
        Started by geddyisodin, Today, 05:20 AM
        2 responses
        17 views
        0 likes
        Last Post geddyisodin  
        Started by Max238, Today, 01:28 AM
        5 responses
        45 views
        0 likes
        Last Post Max238
        by Max238
         
        Started by giulyko00, Yesterday, 12:03 PM
        3 responses
        13 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by habeebft, Today, 07:27 AM
        1 response
        16 views
        0 likes
        Last Post NinjaTrader_ChristopherS  
        Working...
        X