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

Passing a Property to a method using a reference

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

    Passing a Property to a method using a reference

    Hello:

    I believe this question is more related to doing something in C# then NinjaScript.

    I am attempting to pass a property to a method as a reference which apparently is not allowed in C#. However, from what I've read there is a workaround, but it way over my head.

    I have a working stubbed out version of the indicator under development which can be used for testing but to date have not been able to figure this out. There is a a lot of redundant code in the real indicator that could be eliminated by using a method call. If someone can lend some assistance I will gladly provide more detailed explanation and the test code.

    Thank you,

    Frank
    Last edited by Cheech; 03-01-2017, 12:53 PM.

    #2
    Hello Frank,

    Could you provide a small example of the property you are trying to pass?

    A return method would likely work the best but it would also depend on what logic you need to accomplish in the method and if you are already returning a value that is not the value of the property in question.

    A simple example is the following return method:

    Code:
    protected override void OnBarUpdate()
    {
    	double myPrice = DoubleMyPrice(Close[0]);
    }
    
    private double DoubleMyPrice(double price)
    {
    	return price * 2;	
    }

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hmmm, seems like my last response didn't quite make it..

      Anyway Jesse thank you for responding.

      The property is an ExpansionObject (EO) that you helped me with the other day, of which I have quite a few of them in the properties section. There is code to extract the data from each of these EOs which is very redundant that I would like to optimize by putting this code into a method and passing the appropriate ExpansionObject to the method

      Th existing code to extract the data is as follows:

      maFastestPeriod = P0_PeriodSetting.A_FastestPeriod;
      maFastPeriod = P0_PeriodSetting.B_FastPeriod;
      ...



      The ExpansionObject(s) are defined as P0_PeriodSetting, P1_PeriodSetting, ...Pn_PeriodSetting and would be passed to the method by reference and the code in the method would extract the data and put it into the variables (like above) that are used in other parts of the indicator.

      I have uploaded a stub version (working) of the indicator that can be used for testing whatever code is needed. The above code is located at line 191 which one of the places where a method call would be located. There is another just below it.. The method is located at line 390 and is mostly commented out.

      Any help you can give is appreciated.
      Attached Files
      Last edited by Cheech; 03-01-2017, 10:44 PM.

      Comment


        #4
        Hello,

        Thank you for the reply.

        You may be able to pass the object its self as the overload instead of a double, it would just need to match the type of object being passed. Unfortunately, these type of items is more of a general C# item which a C# tutorial is likely better suited for learning. You can locate more examples of passing objects here: https://blog.udemy.com/csharp-pass-by-reference/
        A good search may be "passing object reference to methods C#"


        This also appears to be nearly the same item that we are currently reviewing through email as well, to prevent duplicate replies I will merge this item into your original request and continue to work with you through email.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          Unfortunately the various tutorials found on the web on passing objects by reference do not cover the condition here. Although I have done method calls by reference before I have never tried to do so passing a Property, and no matter what the syntax the compiler is pretty specific that it is not allowed.

          Searching on that specific topic returns some hits where there appears to be a workaround but unfortunately the examples used are not clear to me how to do it and are over my head.

          I did not mean to duplicate our emails efforts on this topic and was hoping that I could connect with someone on the Forum that had already done this before and would provide an example using the stub code I provided.

          I'll keep searching.

          Comment


            #6
            Problem resolved. Really didn't need to do a call by reference.

            Seeing that there is no value in this tread please delete it if you can.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by RubenCazorla, Today, 09:07 AM
            2 responses
            13 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by i019945nj, 12-14-2023, 06:41 AM
            7 responses
            82 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by timmbbo, 07-05-2023, 10:21 PM
            4 responses
            158 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by tkaboris, Today, 08:01 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by Lumbeezl, 01-11-2022, 06:50 PM
            31 responses
            820 views
            1 like
            Last Post NinjaTrader_Adrian  
            Working...
            X