Araxnid
Araxnid

Reputation: 173

Error with adding function in to dictionary (c#)

I'm trying to add function in to dictionary. This works fine:

// public string GetDensity_EdgeZone_LeftHand()
Dictionary<string, Func<string>> FunctionsGetMeasureData;
FunctionsGetMeasureData = new Dictionary<string, Func<string>>();
FunctionsGetMeasureData.Add(tempArea, _MeasureData.GetDensity_EdgeZone_LeftHand);

But, also in this _MeasureData i have public Dictionary<eMeasureProperty, cClassifyData> MeasuresData; and cClassifyData contains function public string GetAverage(string classify)

So, when I'm trying to add it like this:

FunctionsGetMeasureData.Add(tempArea, _MeasuresData.MeasuresData[NameKey].GetAverage(somename);

It all goes to hell. Wheres is problem? I also tryed to work with other type of dictionary Dictionary<string, Func<string, string>> FunctionsGetComplexMeasureData; Did not work too.

Upvotes: 0

Views: 68

Answers (1)

Kenneth
Kenneth

Reputation: 28747

You're not trying to add a function, you're adding the result of that function:

FunctionsGetMeasureData.Add(tempArea, _MeasuresData.MeasuresData[NameKey].GetAverage(somename));

If you want to add the function, you shouldn't invoke it:

FunctionsGetMeasureData.Add(tempArea, _MeasuresData.MeasuresData[NameKey].GetAverage);

Apart from that, if your dictionary is Dictionary<string, Func<string>> you can't add _MeasuresData.MeasuresData[NameKey].GetAverage because it's a Func<string, string>

Upvotes: 2

Related Questions