0

I would like to create a dictionary that uses strings as keys to instantiate objects. Here is the start of my dictionary:

Dictionary<string, ITerminalCommand> validInputs = new Dictionary<string, ITerminalCommand>()
{
    {"help",  TerminalCommandHelp},
    {"exit",  TerminalCommandExit},
};

These terminal command classes implement the ITerminalCommand interface as such:

public class TerminalCommandHelp : MonoBehaviour, ITerminalCommand
{
    //contents of class correctly implementing interface
}

The problem is that when I declare and initialize my dictionary, I'm getting an error saying

"TerminalCommandHelp" is a type, which is not valid in the given context.

I thought interfaces could be use abstractly to represent any class that implements from it? Ultimately, when the user looks up a key, I want to create an instance of that particular class. Can someone point out my misunderstanding? Thank you!

Vranvs
  • 1,411
  • 4
  • 17
  • 38
  • 1
    You're trying to pass the Type to your dictionary not an initialized object. You need an instance of TerminalCommandHelp and TerminalCommandExit not their names. – Cubicle.Jockey Aug 03 '20 at 19:00
  • 1
    `I thought interfaces could be use abstractly to represent any class that implements from it` that's correct. But the way you are trying to use is not correct. You need to add objects of classes to the dictionary. Not the class themselves. – Chetan Aug 03 '20 at 19:00
  • 1
    Following up on the other correct comments, you need to do `new TerminalCommandHelp()` and `new TerminalCommandExit()`. Demo fiddle here: https://dotnetfiddle.net/6TNeTR – dbc Aug 03 '20 at 19:02
  • I see -- so is there a way to initialize a class from this dictionary by saying something like: new validEntries["help"]()? Or must I only pass in an initialized object, and use that initialized object? I cannot create more this way? – Vranvs Aug 03 '20 at 19:02
  • You are creating a dictionary of `ITerminalCommand` objects, so you need to properly construct each object you add. See [Using Constructors (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors). Or are you really trying to create a dictionary of `ITerminalCommand` **factories**, like the one in [Factory pattern, Avoid same switch case for different interface](https://stackoverflow.com/a/50927254/3744182)? – dbc Aug 03 '20 at 19:06
  • I am not formally trained in C#, but I think a factory is what I want. In essence, when I lookup an entry in the dictionary via the "help" key, I want to instantiate a new TerminalCommandHelp object and pass in a few parameters into the constructor. So, I need I guess an abstract reference to the class/type, instead of a pre-initialized object, if that makes sense? – Vranvs Aug 03 '20 at 19:08
  • Then please [edit] your question to clarify. Possibly [Factory pattern, Avoid same switch case for different interface](https://stackoverflow.com/q/50927069/3744182) is a duplicate though, just use `Dictionary>` instead of `Dictionary>`. – dbc Aug 03 '20 at 19:10

1 Answers1

0
//You are trying to pass their type not an instance.
Dictionary<string, ITerminalCommand> validInputs = new Dictionary<string, ITerminalCommand>()
    {
        {"help",  TerminalCommandHelp},
        {"exit",  TerminalCommandExit},
    };

//Initialize your types into objects and put those in your Dictionary.
IDictionary<string, ITerminalCommand> validInputs = new Dictionary<string, ITerminalCommand>()
    {
        {"help",  new TerminalCommandHelp()},
        {"exit",  new TerminalCommandExit()},
    };
Cubicle.Jockey
  • 3,288
  • 1
  • 19
  • 31