Argument error: Converting from Task to string

15367
3
02-23-2022 10:34 PM
tzz_12
by
Emerging Contributor

I am not sure why I keep getting the conversion error that states, "Cannot convert from 'System.Threading.Task.Task<string> to string" when I tried to return a list of string values as a argument parameter for another method. Please see my script below: 

//Looking up a list of string values, mukeys 

public async Task<List<string>> LookupMukey()
{
var mukeys = new List<string>();
var MV = MapView.Active;
int mukeyCount = 0;
var SelectionLayer = MV.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(fl => fl.Name.Contains("SelectionLayer")).FirstOrDefault();

await QueuedTask.Run(() =>
{
using (var SelectionTable = SelectionLayer.GetTable())
{
using (var rowCursor = SelectionTable.Search())
{
while (rowCursor.MoveNext())
{
using (Row row = rowCursor.Current)
{
var mukey = Convert.ToString(row["mukey"]);
mukeys.Add(mukey);
mukeyCount++;
}
}
}
}

});
return mukeys;
}

//calling the list of mukeys  and outputting the values into a text file (performed by the function, //OutputTextFiles() )

Utilities helperclass = new Utilities();
var strOutput = helperclass.LookupMukey();
helperclass.OutputTextFiles(strOutput, "TestFile");    //error occurs at strOutput

Thanks for your help!

 

Tags (3)
0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor

You have to use the await when calling returning a Task<TResult>. See this documentation.

var strOutput = await helperclass.LookupMukey();

 

0 Kudos
ThiPham12
Emerging Contributor

I am able to await for the method, LookupMukey and obtain a list of mukeys, but when I output the string of mukeys, there is an error that  states,"Cannot convert from 'System.Threading.Task.Task<string> to string." 

0 Kudos
BerndtNording
Regular Contributor

Awaiting the  LookupMukey function returns a task not, what the function itself returns. In order to get the list that  LookupMukey produces you need

List<string> strOutput = await helperclass.LookupMukey().Result;

 

0 Kudos