Reputation:
In my code I am reading from a json file in one class and needing it to return an object I created.
The object I entitled StockData (its a stock with symbol, name, price, etc..)
In page 2 I call a method from page 1 to 'getStockData()'.
The method looks similar to:
public async Task<StockData> getStockData()
{
string apiString = await apiConnection.DownloadStringTaskAsync(API);
// ... important code is run ...
// ... newStock is created as a new StockData() type ...
// returning newStock, which returns as a Task<StockData>
return newStock;
}
Unfortunately, for obvious reasons, I cannot just return a StockData because an asynchronous method does not work that way.
However, on page two where the method is called from, I need to figure out some kind of way to convert it away from the task...
Here is an example of what I have on the other side:
StockData newStock = dm.getStockData();
Now I understand why its not working. So I don't need an explanation as to why my code isn't working. I just don't have a solution to get it to work.
Does anyone know how I can get this to work? Any work arounds or adjustments to my code I can do while still allowing the method to run asynchronously?
Many thanks ahead! :)
Upvotes: 0
Views: 49
Reputation: 26342
Use the await keyword?
StockData newStock = await dm.getStockData();
The operation will execute asynchronously and the return value will be of StockData
type.
Upvotes: 2