Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There is no argument given that corresponds to the required formal parameter 'value' of Dictionary<string, string>.Add(string, string) [duplicate]

Tags:

c#

dictionary

I try to construct an instance of Dictionary<string, Dictionary<string, string>> in this way,

Dictionary<string,  Dictionary<string, string>> PredictiveTable =
            new Dictionary<string, Dictionary<string, string>>(){
                {"+", new Dictionary<string, string>() {"E","error"}},
                {"*", new Dictionary<string, string>() {"E","error"}},
                {"i", new Dictionary<string, string>() {"E","E->TK"}},
                {"(", new Dictionary<string, string>() {"E","E->TK"}},
                {")", new Dictionary<string, string>() {"E", "error"}},
                {"#", new Dictionary<string, string>() {"E", "error"}}
};

but the compiler just tell the error messages

CS7036 C# There is no argument given that corresponds to the required formal parameter 'value' of Dictionary<'string, string>.Add(string, string)

And I try to search the error tips before, but just can't find a way to solve it.

I will be sincerely appreciated if anyone could help me.

like image 999
Bowen Peng Avatar asked Apr 17 '18 08:04

Bowen Peng


2 Answers

Dictionary<string, Dictionary<string, string>> PredictiveTable =
            new Dictionary<string, Dictionary<string, string>>(){
                {"+", new Dictionary<string, string>() {{"E","error"}}},
                {"*", new Dictionary<string, string>() {{"E","error" }}},
                {"i", new Dictionary<string, string>() {{ "E","E->TK"}}},
                {"(", new Dictionary<string, string>() {{"E","E->TK"}}},
                {")", new Dictionary<string, string>() {{"E", "error"}}},
                {"#", new Dictionary<string, string>() {{ "E", "error"}}}
};

new Dictionary<string, string>() expects a collection of KeyPairValues for the initialization (like you did for the outer Dictionary). You passed only one.

So

new Dictionary<string, string>() {"E","error"}

becomes

new Dictionary<string, string>() {
    {"E","error"}
}
like image 112
Hyarus Avatar answered Oct 19 '22 23:10

Hyarus


Try to follow this syntax.

In your code, you are missing {} brackets while initializing sub dictionary with values. The same you did for out Dictionary but not in inner.

Dictionary<string, Dictionary<string, string>> dict = new Dictionary<string, Dictionary<string, string>>() {
                {"Key1", new Dictionary<string, string>() { { "SubKey1","Value1"} } },
                {"Key2", new Dictionary<string, string>() { { "SubKey2","Value2"} } }
            };

After following this, your Dictionary will look like

Dictionary<string, Dictionary<string, string>> PredictiveTable =
            new Dictionary<string, Dictionary<string, string>>(){
                {"+", new Dictionary<string, string>() {{"E","error"}}},
                {"*", new Dictionary<string, string>() {{"E","error" }}},
                {"i", new Dictionary<string, string>() {{ "E","E->TK"}}},
                {"(", new Dictionary<string, string>() {{"E","E->TK"}}},
                {")", new Dictionary<string, string>() {{"E", "error"}}},
                {"#", new Dictionary<string, string>() {{ "E", "error"}}}
};
like image 21
Gaurang Dave Avatar answered Oct 20 '22 01:10

Gaurang Dave