2012-11-13 47 views
1

返回的值我使用的是類不能訪問通過函數

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」是沒有得到返回的值分配.... 。

有沒有解決方法?

+1

你爲什麼初始化'chartValues'給你那麼不使用反正值?你的'getCashFlowChartValues'方法在調用'cashFlowChart'後真的停止了嗎?如果是這樣,它有什麼意義呢?基本上我們需要更多的上下文 - 目前你的代碼沒有意義。 –

+0

@JonSkeet不,我必須通過返回的值。但在此之前,我需要得到正確的值。在開發函數 – Anjana

+1

中第一次設置'chartValues'是多餘的,因爲你正在分配一個新的值在下一行。也許有什麼事情發生在你將'cashFlowChart'編碼去掉的地方,因爲那部分代碼看起來很好。 –

回答

1

基於方法的名字 - 「getCashFlowChartValues」和返回類型 - 「空缺」,這是我的猜測 -

你需要移動「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; 
    }  
} 
+0

非常感謝。這工作在代碼..... – Anjana

-1

你創建兩個對象,一旦在該行的getCashFlowChartValues梅索德:

List<KeyValuePair<string, string>> chartValues= 
       new List<KeyValuePair<string,string>>(); 

,一旦在行梅索德cashFlowChart

List<KeyValuePair<string, string>> cashFlowItems = 
       new List<KeyValuePair<string, string>>(); 

嘗試直接調用subfinction這樣的:

List<KeyValuePair<string, string>> chartValues = cashFlowChart (userid); 

林不知道這會解決你的問題b更清潔。

UPDATE:

我只是測試下面的代碼和它的工作對我來說,即使是在原來的版本時,這兩個對象ceated它的工作對我來說,和returnes預期的輸出,我只想設置一個斷點,採取更深的一面,猜你會發現它的工作。莫比根據代碼的其他行你沒有得到預期的輸出:

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; 
     }  
+0

這不是問題的答案 - 這與RenéWolferink和我已經提出的評論基本相同,但*作爲*評論... –

+0

@Jon:我明確寫道,這可能無法解決問題 – CloudyMarble

+1

是的,你做了 - 這並不使它更適合添加爲答案。 –