我正在使用课程
public partial class CashFlowIndicator:IEnumerable
{
public static void getCashFlowChartValues(int userid)
{
List<KeyValuePair<string, string>> chartValues=new List<KeyValuePair<string,string>>();
chartValues = cashFlowChart(userid);
}
public static List<KeyValuePair<string, string>> cashFlowChart(int userid)
{
//.............................
//..................................
List<KeyValuePair<string, string>> cashFlowItems = new List<KeyValuePair<string, string>>();
cashFlowItems.Add(new KeyValuePair<string, string>(a, b));
cashFlowItems.Add(new KeyValuePair<string, string>(c, d));
//.....................
return cashFlowItems;
}
}
问题是在调用函数cashFlowChart时,变量“chartValues”没有被返回的值赋值.....
有什么解决方案吗?
答案 0 :(得分:1)
基于方法名称 - “getCashFlowChartValues”,并返回类型 - “void”,这是我的猜测 -
你需要在方法之外移动“chartValues”的定义并使其“静态”(如下所示):
public class CashFlowIndicator :IEnumerable
{
static List<KeyValuePair<string, string>> chartValues; // has been moved here from getCashFlowChartValues
public static void getCashFlowChartValues(int userid)
{
//List<KeyValuePair<string, string>> chartValues=new List<KeyValuePair<string,string>>();
chartValues = cashFlowChart(userid);
}
public static List<KeyValuePair<string, string>> cashFlowChart(int userid)
{
//.............................
//..................................
var cashFlowItems = new List<KeyValuePair<string, string>>();
cashFlowItems.Add(new KeyValuePair<string, string>(a, b));
cashFlowItems.Add(new KeyValuePair<string, string>(c, d));
//.....................
return cashFlowItems;
}
}
答案 1 :(得分:-1)
您正在创建两个对象,一次在行的getCashFlowChartValues
方法中:
List<KeyValuePair<string, string>> chartValues=
new List<KeyValuePair<string,string>>();
并且在行中的方法cashFlowChart
中进行一次:
List<KeyValuePair<string, string>> cashFlowItems =
new List<KeyValuePair<string, string>>();
尝试直接调用子图像:
List<KeyValuePair<string, string>> chartValues = cashFlowChart (userid);
我不确定这会解决你的问题,但更清洁。
更新:
我刚刚测试了以下代码并且它正在为我工作,即使在原始版本中,当两个对象停止为我工作时,并返回预期的输出,我只是设置一个断点来深入了解,猜测你会发现它的工作。 Maby根据代码中的其他行你得不到预期的输出:
class Program
{
static void Main(string[] args)
{
foreach (KeyValuePair<string, string> s in getCashFlowChartValues())
{
System.Console.WriteLine(s.Key + s.Value);
}
System.Console.ReadLine();
}
public static List<KeyValuePair<string, string>> getCashFlowChartValues()
{
List<KeyValuePair<string, string>> chartValues = cashFlowChart(5);
return chartValues;
}
public static List<KeyValuePair<string, string>> cashFlowChart(int userid)
{
//.............................
//..................................
List<KeyValuePair<string, string>> cashFlowItems = new List<KeyValuePair<string, string>>();
cashFlowItems.Add(new KeyValuePair<string, string>("a", "b"));
cashFlowItems.Add(new KeyValuePair<string, string>("c", "d"));
//.....................
return cashFlowItems;
}