2014-08-27 3 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; 코드를 관련 부분으로 압축하십시오. – thumbmunkeys

+0

전체 프로젝트를 게시하지 마십시오. 관련 코드 만 게시하십시오. –

+0

또한 클래스 :'CurrencyDic'에는'Add' 메소드가 없습니다. – derape

답변

0

당신이 currencyDic 다른 사전을 보인다.

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

+0

currencyDic 자체는 Dictionary () – tanuk

+0

입니다. 그는 자신이 만든 CurrencyDic 클래스를 객체에서 상속 받았으며 그 클래스에는 'currencyDic' 필드가 포함되어 있습니다. 이것은'Dictionary '입니다. –

관련 문제