C#传递值从一个方法到另一个

编程入门 行业动态 更新时间:2024-10-09 21:25:38
本文介绍了C#传递值从一个方法到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我得到的结果错误代码。下面的代码。我基本上想从 SingleColumn 方法的数据集,并在 SMA 方法使用它。但我在目前的情况下不存在得到结果DEOS。

的静态公共无效SMA() {双击[] = closePrice results.ToArray();

下面你可以看到 SingleColumn 和零件的 SMA 代码。

#地区的单立柱 //静态公共双重效果; 静态公共无效SingleColumn(IEnumerable的<串>可疑交易报告,INT highNum) {#地区泻数据列 Console.WriteLine(单列查询:); VAR columnQuery =从可疑交易报告线让元素= line.Split('')选择Convert.ToDouble(元素[highNum]); 变种结果= columnQuery.ToList(); 双[] = closePrice results.ToArray(); #endregion #地区的最大值,最小值,平均值双平均= results.Average(); 双最大值= results.Max(); 双分钟= results.Min(); Console.WriteLine(高:{0}:低:{1}:平均:{2:0.00},最大值,最小值,平均值); #endregion } #地区的战略码SMA 静态公共无效SMA() {双[] = closePrice结果。 ToArray的(); INT TOTAL_PERIODS = closePrice.Length; 双[] =输出新的双[TOTAL_PERIODS] INT开始; INT长度; 的for(int i = 0; I< closePrice.Length-TOTAL_PERIODS;我++)//不得不从-1改为-TOTAL_PERIODS { closePrice [I] =(双)我; } TicTacTec.TA.Library.Core.RetCode RETCODE = Core.Sma(0,closePrice.Length-1,closePrice,PERIODS_AVERAGE,开始出来,出来的长度,输出);

解决方案

您有一些选择:

  • 有 SingleColumn 的收益的结果并补充说,作为参数传递给 SMA
  • 请结果类的字段,以便它是共享的。
  • 选项1是更清洁,因为它迫使主叫方呼叫 SingleColumn 第一(或拿出自己的列表)

    静态公网双[] SingleColumn (IEnumerable的<串GT;可疑交易报告,INT highNum) {:返回closePrice; } #地区的战略码SMA 静态公共无效SMA(双[] closePrice) { INT TOTAL_PERIODS = closePrice.Length; 双[] =输出新的双[TOTAL_PERIODS] :}

    请注意,我是从改变了你的输出/输入结果到 closePrice ,因为它只是将其转换为一个List和背部。这只是清洁离开它作为一个双[] 。您也可以只使用清理代码位的的ToArray 而不是使用了ToList 和然后 的ToArray 。

    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. But I am getting results deos not exist in current context.

    static public void SMA() { double[] closePrice = results.ToArray();

    Below you can see the SingleColumn and part of SMA code.

    #region Single Column //static public double results; static public void SingleColumn(IEnumerable<string> strs, int highNum) { #region spilt data to columns Console.WriteLine("Single Column Query:"); var columnQuery = from line in strs let elements = line.Split(',') select Convert.ToDouble(elements[highNum]); var results = columnQuery.ToList(); double[] closePrice = results.ToArray(); #endregion #region max, min, avg double average = results.Average(); double max = results.Max(); double min = results.Min(); Console.WriteLine("High: {0}: Low: {1}: Average: {2:0.00}", max, min, average); #endregion } #region Strategy Code SMA static public void SMA() { double[] closePrice = results.ToArray(); int TOTAL_PERIODS = closePrice.Length; double[] output = new double[TOTAL_PERIODS]; int begin; int length; for (int i = 0; i < closePrice.Length-TOTAL_PERIODS; i++) //had to change from -1 to -TOTAL_PERIODS { closePrice[i] = (double)i; } TicTacTec.TA.Library.Core.RetCode retCode = Core.Sma(0, closePrice.Length-1, closePrice, PERIODS_AVERAGE, out begin, out length, output);

    解决方案

    You have some options:

  • have SingleColumn return results and add that as a parameter to SMA
  • 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.

    更多推荐

    C#传递值从一个方法到另一个

    本文发布于:2023-11-05 02:47:47,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1559700.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:方法

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!