2014-08-27 73 views
-5

我得到了一個列表,即時嘗試改變字典(它是必須的,所以我不能使用列表)的代碼。我的問題是,在'BankRates.cs'中,我無法將對象添加到我的字典中。我知道添加也應該得到一個字符串,以便程序正常工作,但我只是無法弄清楚什麼是我應該在'添加'方法中插入的關鍵。無法初始化字典在c#

我有這個4 CS文件:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Converter 
{ 
    class Currency 
    { 
     //members 

     //getters and setters 
     public string Code { get; set; } 
     public string Name { get; set; } 
     public double Rate { get; set; } 
     public double Unit { get; set; } 
     public string Country { get; set; } 

     //constractor 
     public Currency(string code, string name, double rate, double unit, string country) 
     { 
      this.Code = code; 
      this.Name = name; 
      this.Rate = rate; 
      this.Unit = unit; 
      this.Country = country; 
     } 

     //override ToString method for visualization  
     public override string ToString() 
     { 
      return (Name + "-" +Code); 
     } 
    } 
} 

currencyDictionary:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Converter 
{ 
    class CurrencyDic 
    { 
     //setter and getter 
     public Dictionary<string,Currency> currencyDic { get; set; } 
     //constractor 
     public CurrencyDic() 
     { 
      currencyDic = new Dictionary<string,Currency>(); 
     } 
     public CurrencyDic(Dictionary<string,Currency> cur) 
     { 
      currencyDic = new Dictionary<string,Currency>(cur); 
     } 
     // implements foreach 
     public IEnumerator<Currency> GetEnumerator() 
     { 
      foreach (Currency cur in currencyDic.Values) { yield return cur;} 
     } 
    } 
} 


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Converter 
{ 
    interface IBankRates 
    { 
     void GetRates(); 
     double Convert(Currency from, Currency to, double amount); 
    } 
} 

,最後一個:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.IO; 
using System.Threading.Tasks; 
using System.Xml; 
using System.Xml.Linq; 
using System.Runtime.Remoting.Messaging; 



namespace Converter 
{ 
    class BankRates:IBankRates 
    { 
     private string Bank_URL = "http://www.boi.org.il/currency.xml"; 
     CurrencyDic currencyDic = new CurrencyDic(); 
     public void GetRates() 
     { 
      XDocument xdoc = new XDocument(); 
      try 
      { 
       xdoc = XDocument.Load(Bank_URL);} 
       catch (XmlException) 
      { 
       MessageBox.Show("Failed to load Xml file"); 
       System.Environment.Exit(1); 

      } 
       //load the xml 
       var allCurencies = from currency in xdoc.Descendants("CURRENCY")  //linq query 
            select new 
            { 
             Name = currency.Descendants("NAME").First().Value, 
             Unit = currency.Descendants("UNIT").First().Value, 
             Code = currency.Descendants("CURRENCYCODE").First().Value, 
             Cuntry = currency.Descendants("COUNTRY").First().Value, 
             Rate = currency.Descendants("RATE").First().Value 
            }; 


       foreach (var currency in allCurencies)         //create the currency list 
       { 
        currencyDic.Add(****What Goes Here?****,new Currency(currency.Code, currency.Name, double.Parse(currency.Rate), double.Parse(currency.Unit), currency.Cuntry)); 
        // Console.WriteLine(currency.Code + currency.Cuntry + currency.Name, currency.Rate + currency.Unit); 
       } 


     } 
     //returns the list 
     public CurrencyDic getDic() 
     { 
      return currencyDic; 
     } 

     //makes the converting calculation 
     public double Convert(Currency from,Currency to, double amount) 
     { 
      double inRate, outRate, excangeRate; 
      inRate = from.Rate/from.Unit; 
      outRate = to.Rate/to.Unit; 
      excangeRate = inRate/outRate; 
      return (amount * excangeRate); 
     } 
    } 
} 
+0

TL; DR;將代碼壓縮到相關部分 – thumbmunkeys 2014-08-27 11:11:21

+0

不要發佈整個項目。僅發佈相關代碼。 – 2014-08-27 11:12:33

+0

另外:你的類'CurrencyDic'甚至沒有'Add'方法 – derape 2014-08-27 11:12:40

回答

0

看來你有currencyDic另一個字典。

this.currencyDic.currencyDic.Add(「string」,「some object Currency」);

+0

currencyDic本身是一個字典() – tanuk 2014-08-27 11:19:32

+0

他得到了一個類'CurrencyDic',他創建的只是從對象繼承而來,並且該類包含一個字段'currencyDic'這是一個'Dictionary '。 – 2014-08-27 11:26:36