How to access VIP in the proxy_OpenReadCompleted method?
void method1()
{
String VIP = "test";
WebClient proxy = new WebClient();
proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(proxy_OpenReadCompleted);
String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
}
void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
}
There are two approaches to this. First is to pass the string as the second parameter in the OpenReadAsync
call, this parameter becomes the value of the UserState
property of the event args.
void method1()
{
String VIP = "test";
WebClient proxy = new WebClient();
proxy.OpenReadCompleted += proxy_OpenReadCompleted;
String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
proxy.OpenReadAsync(new Uri(urlStr, UriKind.Relative), VIP);
}
void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
String VIP = (string)e.UserState;
// Do stuff that uses VIP.
}
Another approach is to access the variable directly using a closure:-
void method1()
{
String VIP = "test";
WebClient proxy = new WebClient();
proxy.OpenReadCompleted += (s, args) =>
{
// Do stuff that uses VIP.
}
String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
proxy.OpenReadAsync(new Uri(urlStr, UriKind.Relative), VIP);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With