1

I have a Async method that returns Boolean value but when I am calling that function and trying to get the value as bool , I am getting an error as:

cannot implicitly convert type 'system.threading.tasks.task bool ' to 'bool'.

Here is how I am calling my async method:

bool IsAvailable = Helper.search(Name);

How to correct it?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Lara
  • 2,821
  • 7
  • 39
  • 72

2 Answers2

6

It should be

bool IsAvailable = await AdHelper.searchGroup(Name);

There is a good example here http://www.dotnetperls.com/async.

If you can't await it, you can check this answer here.

Community
  • 1
  • 1
Radin Gospodinov
  • 2,313
  • 13
  • 14
  • Okay . But for that i have to make my function as `async` which i dont have permission to do ..Without that is there any way ..pls – Lara Dec 29 '15 at 08:44
  • 1
    You can use AdHelper.searchGroup(Name).Result. But this will make you call synchronous and will block the calling thread. The alternative is to use AdHelper.searchGroup(Name). ContinueWith and to set the IsAvailable var in the passed delegate. Before editing you code I suggest you to read this https://msdn.microsoft.com/en-us/magazine/jj991977.aspx. – Radin Gospodinov Dec 29 '15 at 08:45
  • @Lara check here: http://stackoverflow.com/a/15524271/2524010 – mehmet mecek Dec 29 '15 at 08:54
2

Just expanding on Radin's answer;

AdHelper.searchGroup(Name) - the value you were trying to assign, is a task, thus you got the error:

cannot implicitly convert type 'system.threading.tasks.task bool' to 'bool'

What you probably wanted is the value that the task returns. In order to get an async value out of a task, you need to use the keyword await.

await - an operator which is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.

So, in order to get the value out of the async task, you need to use -

bool IsAvailable = await AdHelper.searchGroup(Name);
A. Abramov
  • 1,823
  • 17
  • 45