Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

C# function array and passing data to it

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

    C# function array and passing data to it

    When using a function in C#, do you have to pass data in brackets?

    If want to send data, double [,] dist, to the function, do i need to include it in the brackets describing the function

    the int remdim is being picking up, even though its not in the function, it is making me think I dont need to include double [,] dist in the function


    thanks

    Code:
    public static double calcdist(double [,] dist,double [,]coord)
    {
    int nofrows=5;  
        for (int i = 0; i< nofrows; i++) 
        {
            for (int j = 0; i< nofrows; i++) 
            {
                        dist[i, j] = 0;
                        for (int k = 0; k<remdim;k++)
                        {
                        dist[i, j] = dist[i, j] + Math.Pow((coord[i, k] - coord[j, k]),2);
                        }
                        dist[i, j] = dist[i, j]*dist[i,j];//square
            }
        }
    return 1.00;    
    }

    #2
    Hi Tinkerz,

    This is C# related so outside our scope of NinjaScript support. Hopefully other community members can offer input for you here.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Tink,

      First, do you get any compile errors when you try to compile this code?

      Also, you are manipulating the contents of dist[,]. In order to do this you need to pass by reference. Search a bit via google, you will find plenty of examples.
      mrlogik
      NinjaTrader Ecosystem Vendor - Purelogik Trading

      Comment


        #4
        Yes I am now using the ref command, seems ok, compiles

        Comment


          #5
          Pass by reference semantics

          Originally posted by tinkerz View Post
          Yes I am now using the ref command, seems ok, compiles
          I don't think passing by reference is your issue as you are manipulating an array of doubles which already uses pass by reference semantics (standard C# behaviour).

          Just my 2c..

          Comment


            #6
            Just expanding on that with a random c# sample:

            Assume you have a function with the following signature:

            publicstaticdouble calcdist(double[,] dist) { <code in here> }

            You can call it like this:

            var t = new double[1,1];
            calcdist(t);


            Assuming your function is the following:


            publicstaticdouble calcdist(double[,] dist) { dist[0,0] = 10; }


            If you call it like this:

            var t = new double[1,1];
            t[0,0] = 3;
            Console.WriteLine(t[0,0]);
            calcdist(t);
            Console.WriteLine(t[0,0]);

            You will get the following results:

            3
            10

            Techical reasons aside, with 'complex objects' in c# such as array, collections etc you are not passing a copy of the variable into the function you are passing in a reference (or pointer) to the variable (known as pass by reference semantics).

            Comment


              #7
              According to MSDN C#

              The ref keyword causes arguments to be passed by reference. The effect is that any changes to the parameter in the method will be reflected in that variable when control passes back to the calling method

              So I assumed when you pass the array in to the function it has data in it, then it it changed by the function, then when you function ends, the array you pass in is changed

              Comment


                #8
                Correct. However, arrays are a reference type and hence behave that way by default and the ref keyword is not required. The ref keyword is normally used with value types (int, double etc) to force them to use reference semantics.

                http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx See Passing Reference-Type Parameters. In example 4 int[] is identified as a reference type.

                Comment


                  #9
                  Sorry I am confused, this complies, but is it correct ie the fastest/easiest?


                  Code:
                          Rvalue=calcdist(ref dist,ref coord);

                  Code:
                  public static double calcdist(ref double [,] dist,ref double [,]coord) 
                  {
                  int nofrows=5;  
                      for (int i = 0; i< nofrows; i++) 
                      {
                          for (int j = 0; i< nofrows; i++) 
                          {
                                      dist[i, j] = 0;
                                      for (int k = 0; k<remdim;k++)
                                      {
                                      dist[i, j] = dist[i, j] + Math.Pow((coord[i, k] - coord[j, k]),2);
                                      }
                                      dist[i, j] = dist[i, j]*dist[i,j];//square
                          }
                      }
                  return 1.00;    
                  }

                  Comment


                    #10
                    Sorry! Was just trying to clarify the use of the ref keyword.. probably just ended up confusing the issue!

                    I was just pointing out that ONLY changing the parameters to ref wouldn't solve your issue, it must've been some other problem. What you have should work fine..

                    Comment

                    Latest Posts

                    Collapse

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