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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        152 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        89 views
        1 like
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        133 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        127 views
        1 like
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        107 views
        0 likes
        Last Post CarlTrading  
        Working...
        X