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 Hwop38, 05-04-2026, 07:02 PM
        0 responses
        160 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Started by CaptainJack, 04-24-2026, 11:07 PM
        0 responses
        307 views
        0 likes
        Last Post CaptainJack  
        Started by Mindset, 04-21-2026, 06:46 AM
        0 responses
        244 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        348 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        178 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Working...
        X