C# passing values from one method to another

后端 未结 3 1834
北荒
北荒 2021-01-17 01:38

I am getting an error code in the result. code below. I am basically trying to get a data set from SingleColumn method and use it in SMA method. Bu

相关标签:
3条回答
  • 2021-01-17 02:15

    You should use static variable.

    private static IEnumerable<double> result;
    

    and then in method SingleColumn asign columnQuery.ToList() to this variable

    0 讨论(0)
  • 2021-01-17 02:26

    Each variable in C# is exists within a scope which is defined by curly braces. In your case the variable result is within the scope SingleCloumn.

    public static void SingleColumn(IEnumerable<string> strs, int highNum)
    {
    
    }
    

    To use result in another scope, you can make "result" as a global variable. As I can see you have commented out

    //static public double results; 
    

    first un-comment it and remove var from

    var results = columnQuery.ToList();
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-17 02:27

    You have some options:

    1. have SingleColumn return results and add that as a parameter to SMA
    2. make results a field of the class so it is shared.

    Option 1. is cleaner since it forces callers to call SingleColumn first (or come up with a list on their own)

        static public double[] SingleColumn(IEnumerable<string> strs, int highNum)
        {
            ...
            return closePrice;
        }
    
            #region Strategy Code SMA
        static public void SMA(double[] closePrice)
        {
            int TOTAL_PERIODS = closePrice.Length;
            double[] output = new double[TOTAL_PERIODS];
            ...
        }   
    

    Note that I changed your output/input from result to closePrice since it was just converting it to a List and back. It's cleaner just to leave it as a double[]. You can also clean up the code a bit by just using ToArray instead of using ToList and then ToArray.

    0 讨论(0)
提交回复
热议问题