serge
serge

Reputation: 15249

Initialize a ViewDataDictionary

I need to build a function that takes a parameter and passes it to the System.Web.Mvc.ViewDataDictionary constructor. This is my code:

// this WORKS, but need to pass as parameter
var dic1 = new System.Web.Mvc.ViewDataDictionary { { "key", "value" } };

// this DOES NOT WORK
Test({ "key", "value" }); // I try do not have 'new Something({"", ""})'

function Test(object myParam) {
    var dic2 = new System.Web.Mvc.ViewDataDictionary { myParam };
    //...
}

Is there a way to pass the parameter like I did, without need to pass the entire
new System.Web.Mvc.ViewDataDictionary { { "key", "value" } } as parameter to the Test function?

PS. This is some test code explaining the problem

// WORKS
var dic1 = new System.Web.Mvc.ViewDataDictionary { { "key", "value" } };

// DOES NOT WORK

// A CASE
string[] myArr = { "key", "value" }; // OK
var dic2 = new System.Web.Mvc.ViewDataDictionary { myArr }; // NOK

// B CASE    
KeyValuePair<string, object> myPair = {"key", "value"}; // NOK
var dic3 = new System.Web.Mvc.ViewDataDictionary { myPair }; // OK

PPS. multiple arguments case:

// WORKS
var dic1 = new ViewDataDictionary { { "key", "value" }, {"key2", "value2"} };
var dic2 = new ViewDataDictionary { (replace w/ one single obj/arr impossible?)};

Upvotes: 0

Views: 1680

Answers (1)

Sergii Zhevzhyk
Sergii Zhevzhyk

Reputation: 4202

Unfortunately, it's not possible to achieve what you want because curly brackets can be used only for the collection initialization. The parser treats these curly brackets in a special way only if they are connected to the new keyword. It parses them and generates a bunch of the Add calls.

If you want to pass only key and value it can be done like this:

Test("key", "value"); 

void Test(string key, object value) {
    var dic2 = new ViewDataDictionary { { key, value  } };
    //...
}

The ViewDataDictionary contains a constructor which accepts another ViewDataDictionary:

Test(new ViewDataDictionary { { key, value  } }); 

void Test(ViewDataDictionary dictionary) {
     var dic2 = new ViewDataDictionary(dictionary);
     //...
}

If you need to add some values from one ViewDataDictionary to another existing ViewDataDictionary you could use the following code:

Test(new ViewDataDictionary { { key, value  } }); 

void Test(ViewDataDictionary dictionary) {
     var dic2 = new ViewDataDictionary();
     // ... initialize dic2
     foreach (var pair in dictionary)
     {
            dic2.Add(pair);
     }
}

If there is a need to pass an array (of KeyValuePair elements) to initialize the dictionary it can be done in this way:

Test(new[] { new KeyValuePair<string, object>("key1", "value1"),
             new KeyValuePair<string, object>("key2", "value2") }); 

void Test(KeyValuePair<string, object>[] pairs) {
     var dic2 = new ViewDataDictionary();
     foreach (var pair in pairs)
     {
            dic2.Add(pair);
     }
     //...
}

Upvotes: 2

Related Questions