Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have this error: "Cannot implicitly convert type 'bool' to 'system.threading.tasks.task bool'" in my service implementation code. Could you correct my code please.

public Task<bool> login(string usn, string pwd)     {         DataClasses1DataContext auth = new DataClasses1DataContext();         var message = from p in auth.Users                       where p.usrName == usn && p.usrPass == pwd                       select p;         if (message.Count() > 0)         {             return true;         }         else         {             return false;         }     } 
like image 476
Peter Avatar asked Jun 07 '14 13:06

Peter


1 Answers

You need to be specific whether you want this operation happen asynchronously or not.

As an example for Async Operation :

public async Task<bool> login(string usn, string pwd) {     DataClasses1DataContext auth = new DataClasses1DataContext();     var message = await (from p in auth.Users                   where p.usrName == usn && p.usrPass == pwd                   select p);     if (message.Count() > 0)     {         return true;     }     else     {         return false;     } } 

If you don't need it to be an Async operation, try this:

public bool login(string usn, string pwd) {     DataClasses1DataContext auth = new DataClasses1DataContext();     var message = from p in auth.Users                   where p.usrName == usn && p.usrPass == pwd                   select p;     if (message.Count() > 0)     {         return true;     }     else     {         return false;     } } 

Note: async and await are compatible with .net 4.5 and C# 5.0 and more

like image 51
Ramy M. Mousa Avatar answered Sep 19 '22 12:09

Ramy M. Mousa