C# 尝试将对象添加到字典时出错
我得到了一个带有列表的代码,我正试图把它改成字典。我的问题是,在“BankRates.cs”中,我无法将对象添加到字典中。我得到错误:没有重载的方法'Add'需要1个参数…有人能解释为什么吗??(我知道我应该向函数中添加一个字符串,但当我试图添加一个空字符串以使其可编译时,字典只包含一个非函数对象) 我有4个cs文件:C# 尝试将对象添加到字典时出错,c#,dictionary,C#,Dictionary,我得到了一个带有列表的代码,我正试图把它改成字典。我的问题是,在“BankRates.cs”中,我无法将对象添加到字典中。我得到错误:没有重载的方法'Add'需要1个参数…有人能解释为什么吗??(我知道我应该向函数中添加一个字符串,但当我试图添加一个空字符串以使其可编译时,字典只包含一个非函数对象) 我有4个cs文件: using System; using System.Collections.Generic; using System.Linq; using System.Text; usi
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);
}
}
}
货币词典:
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);
}
}
字典的条目是一个键和一个值。一个参数会是什么样的信息?@jeroenvanevel一个hashset!你知道
词典是做什么用的吗?你为什么要用一个,而不是一个列表
?你真的应该自己做一点调查。如果你只是在谷歌上搜索“C#Dictionary tutorial”之类的东西,你就可以省下所有发帖的时间。
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)
{
currencyDic.currencyDic.Add(new Currency(currency.Code, currency.Name,
double.Parse(currency.Rate),
double.Parse(currency.Unit), currency.Cuntry));
}
}
//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);
}
}
}