0

when user enter password from login activity sending it to login service and waiting for the boolean response in login service.but its giving above syntax error(I mentioned it in code ) can any one solve my problem. i've to wait until the service response comes to my login activity

1.Login Interface

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RetailAppShared
{
    public interface ILoginServices
    {
        bool AuthenticateUser (string passcode, Func<bool> function);
    }
}

2.Login service

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using RestSharp;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace RetailAppShared
{
    public class LoginServices : ILoginServices
    {               
        public bool AuthenticateUser (string passcode, Func<bool> function)
        {

            try {
                RestClient _client = new RestClient ("https://www.example.com/app/class");
                var request = new RestRequest ("loginservice", Method.POST) {
                    RequestFormat = DataFormat.Json
                };
                var obj = new login ();
                obj.passcode = passcode;

                request.AddJsonBody (obj);
                request.AddHeader ("Content-type", "application/json");
                _client.ExecuteAsync (request, response => {
                    if (response.StatusCode == HttpStatusCode.OK) {
                        if (!string.IsNullOrEmpty (response.Content)) {
                            var objlog = JsonConvert.DeserializeObject<LoginModel> (response.Content);
                            flag = objlog.result.state == 1 ? true : false;

                            function (true);//error
                        } else
                            flag = false;                            
                    }

                });
            } catch (Exception ex) {
                Debug.WriteLine (@" ERROR {0}", ex.Message);
            }               
        }    

    }

    class login
    {
        public string passcode { get; set; }    
    }
}

3.login activity

Login.Click += async (object sender, EventArgs e) => {

    progress = ProgressDialog.Show (this, "", "Connecting...");

    var isLoginSuccessful = loginAuthenticator.AuthenticateUser                
    (password.Text, (value) => {
        Console.WriteLine (value);
    });//error

    if (progress != null) {
        progress.Dismiss ();
        progress = null;
    }

    if (isLoginSuccessful) {                
        StartActivity (typeof(Ledger_HomeActivity));
        this.Finish ();
    } else {
       Toast.MakeText (this, "Invalid Login credentials! try    again", ToastLength.Short).Show ();
    }
};
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
Ramesh yadav
  • 85
  • 1
  • 15
  • 2
    A `Func` is like a method that returns a `bool` but has no parameters. You should call it like `bool result = function();`. If instead you want to pass a `bool` in and have no return values then you should use `Action` instead. – juharr Jul 08 '16 at 11:33

2 Answers2

4

It looks like function represents a callback method that you call with the value of true or false, and that does not return any value back to you. In this case you should Action<bool> instead of Func<bool>, because Func<bool> does the opposite - it takes no parameters, and returns a bool value back to you.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

The error appears at this line

var isLoginSuccessful = loginAuthenticator.AuthenticateUser (password.Text, (value) => { Console.WriteLine (value); }):

A Func<bool> returns a boolean without expecting anything. What you want instead is an Action<bool> that expects a boolean but returns void:

public bool AuthenticateUser (string passcode, Action<bool> function)
{
    function(true);
} 
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111