I am using C# to create a Silverlight 4 application.
I am trying to do the following:
MapNode endNode = null;
if (keyword != null && keyword != "")
{
EntityQuery<NodeIDProj> res = CampusQueries.getNodeIDByNameQuery(keyword);
var queryres = CampusQueries.Load<NodeIDProj>(res, (items) =>
{
foreach (var item in items.Entities)
{
MapNode n = mapHelp.getNodeByID(item.NodeID);
if (n != null)
{
endNode = n;
TrackAnimation();
}
}
}, true);
}
However, after this point, my variable endNode is still null. TrackAnimation() works as though endNode has a valid value, but outside of the Load statement, endNode is back to null.
I know that I am lacking in understanding of how this works, and I would really appreciate an help given.
What I am trying to do, is query my database and I want to use those results in other methods rather than displaying them in a datagrid.
I want endNode to have value which I can use in other methods.
Please help me to figure out a way to do this, thank you!
EDIT:
Thank you, SLaks
Can I do something like:
MapNode endNode = null;
if (keyword != null && keyword != "")
{
EntityQuery<NodeIDProj> res = CampusQueries.getNodeIDByNameQuery(keyword);
var queryres = CampusQueries.Load<NodeIDProj>(res, (items) =>
{
foreach (var item in items.Entities)
{
MapNode n = mapHelp.getNodeByID(item.NodeID);
if (n != null)
{
endNode = n;
TrackAnimation();
}
}
}, true);
}
queryres.Completed += new EventHandler(queryres_Completed);
void queryres_Completed(object sender, EventArgs e)
{
//stuff
}
If so, how can I get access to the endNode variable, as it is declared within another method?
Your
Loadmethod is probably asynchronous, meaning that the callback happens some time after the rest of your code runs.You can only use the result after you actually receive it.