Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

return multiple values in one method

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

    return multiple values in one method

    I would like to have a user defined method that can return multiple values. Is there a way to define it?

    The Ninja Script guide has an example on returning one calculated values. I copied it here:
    // This method performs a calculation and returns a double value
    private double Multiply(double input)
    {
    return input * 10;
    }

    What i want to do is to return 3 values: input*10, input*20, and input*30. Any way to accomplish that? Thanks.

    #2
    A method cannot return more than one value. However you can return an array of values. Unfortunately we do not have the bandwidth to assist with array creations and such. You can try searching google for resources. There are many around demonstrating how to do this.

    For instance http://www.daniweb.com/forums/thread91439.html
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      by reference

      Hi binwang2,

      in case you don't like to construct data structures for result handling you could deliver the parameters by reference. That means they are modified at the source; in this case in the calling environment.

      Regards
      Ralph
      Code:
      [FONT=Courier New][COLOR=#0000ff]private[/COLOR] [COLOR=#0000ff]void[/COLOR] Multiply([COLOR=#0000ff]ref double[/COLOR] input1, [COLOR=#0000ff]ref double[/COLOR] input2)[/FONT] 
      [FONT=Courier New]{[/FONT] 
      [FONT=Courier New]  input1 *= 10;[/FONT]
      [FONT=Courier New]  input2 *= 100;[/FONT] 
      [FONT=Courier New]}[/FONT]
       
      [FONT=Courier New]private [COLOR=blue]double[/COLOR] input1 = 1;[/FONT]
      [FONT=Courier New]private [COLOR=blue]double[/COLOR] input2 = 2;[/FONT]
      [FONT=Courier New]Multiply([COLOR=blue]ref[/COLOR] input1, [COLOR=blue]ref[/COLOR] input2);[/FONT]
      [FONT=Courier New][COLOR=mediumturquoise]// result of input1 = 10[/COLOR][/FONT]
      [COLOR=mediumturquoise][FONT=Courier New]// result of input2 = 200[/FONT]
      [/COLOR]

      Comment


        #4
        Ralph, thanks for pointing out another way.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        598 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        343 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        556 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        555 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X