Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters as a single object?

I have this example to tell you what I'm looking for:

private void myMethod(string a = "", string b = "", string c = "")
{
   // do things
}

I want to find a way where I can call that method like this:

MyParameterObject parameters = new MyParameterObject();
// b is the name of parameter
parameters.AddParameter("b", "b_value");
parameters.AddParameter("c", "c_value");
myMethod(parameters);
like image 940
Adnand Avatar asked Dec 14 '22 21:12

Adnand


1 Answers

If all the parameter values required in the method are of same type(let it be string) then you can pass the parameter as a Dictionary like the following:

private void myMethod(Dictionary<string,string> paramDictionary)
{
   // do things
}

So that you can call the method like this:

Dictionary<string,string> paramDictionary = new Dictionary<string,string>();
paramDictionary.Add("b", "b_value");
paramDictionary.Add("c", "c_value");
myMethod(paramDictionary);
like image 96
sujith karivelil Avatar answered Dec 16 '22 09:12

sujith karivelil