the dictionary "_infoCardFactories" is defined as so:
Dictionary<string, IInfoCardFactory> _infoCardFactories;
the error "the given key was not present in the dictionary" originates from this line of code:
IInfoCard card = _infoCardFactories[category].CreateNewInfoCard(category);
here is the interface for the dictionary as well the class it is implemented into.
public interface IInfoCardFactory
{
IInfoCard CreateNewInfoCard(string category);
IInfoCard CreateInfoCard(string initialDetails);
string[] CategoriesSupported { get; }
string GetDescription(string category);
}
public class creditcard : IInfoCardFactory,IInfoCard
{
public IInfoCard CreateNewInfoCard(string category)
{
creditcard card = new creditcard();
return card;
}
}
i cannot show all of the code, due the fact this is an assigment however i would be greatful if someone could help me out.
You need to implement a class based on this interface and then define your dictionary to use that implementation. There needs to be an instance.
Instead of IInfoCard card = _infoCardFactories[category].CreateNewInfoCard(category);
Use this:
IInfoCard card;
IInfoCardFactory factory;
if (_infoCardFactories.TryGetValue(category, out factory))
card = factory.CreateNewInfoCard(category);
else
{
// when category is not in your dictionary
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.