Question

I have the following scenario:

private void RequestInfo(string a, string b)
{
   var c = a+b;
   library.FetchInfo(c, OnInfoReceived);
}

private void OnInfoReceived(CustomParameterType object)
{
   dictionaryOfInfo.Add(c, object);
}

As you can see, my problem is that I cannot access variable "c" in the callback passed as argument to FetchInfo function.

Était-ce utile?

La solution

In C# 3.0 and later, you can use a lambda expression like this:

private void RequestInfo(string a, string b)
{
   var c = a+b;
   library.FetchInfo(c, obj => dictionaryOfInfo.Add(c, obj));
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top