为什么我收到此消息:无法将类型'bool'转换为'string'

时间:2021-10-02 16:34:59

Below is the code snippet that I am using.

以下是我正在使用的代码段。

using System;
using System.Collections.Generic;
using System.Text;

namespace businessTMS
{
    public class SignIn
    {
        public string authenticate(String UserName, String password)
        {  
            dataTMS.SignIn data = new dataTMS.SignIn();
          string authenticate=(string)data.authenticate(UserName, password);
            return authenticate;
        }

    }
}

2 个解决方案

#1


Your error is occurring due to this line:

由于此行,您的错误正在发生:

string authenticate = (string)data.authenticate(UserName, password);

You are setting authenticate equal to a true/false calculation which is returning a Boolean value. Try this instead.

您将authenticate设置为等于返回布尔值的true / false计算。试试这个。

string authenticate = data.authenticate(UserName, password).ToString();

You can also modify the code to still return a string by doing this:

您还可以通过执行以下操作修改代码以仍返回字符串:

bool authenticate = data.authenticate(UserName, password);
return authenticate.ToString();

PREFERRED OPTION:
Furthermore, I'm not sure why you are returning a string representation of true/false (bool)... If it were my function, I would probably return this:

首选选项:此外,我不知道你为什么要返回true / false(bool)的字符串表示...如果它是我的函数,我可能会返回:

return data.authenticate(UserName, password);

I would highly encourage you to simply return the Boolean value in the PREFERRED OPTION area. There is no obvious reason to keep this in a string format.

我强烈建议您只在“PREFERRED OPTION”区域中返回布尔值。没有明显的理由将其保留为字符串格式。

#2


Use the Boolean.ToString Method (documentation can be found here) like so:

使用Boolean.ToString方法(文档可以在这里找到),如下所示:

using System;
using System.Collections.Generic;
using System.Text;

namespace businessTMS
{
    public class SignIn
    {
        public string authenticate(String UserName, String password)
        {  
            dataTMS.SignIn data = new dataTMS.SignIn();

            return data.authenticate(UserName, password).ToString();
        }

    }
}

#1


Your error is occurring due to this line:

由于此行,您的错误正在发生:

string authenticate = (string)data.authenticate(UserName, password);

You are setting authenticate equal to a true/false calculation which is returning a Boolean value. Try this instead.

您将authenticate设置为等于返回布尔值的true / false计算。试试这个。

string authenticate = data.authenticate(UserName, password).ToString();

You can also modify the code to still return a string by doing this:

您还可以通过执行以下操作修改代码以仍返回字符串:

bool authenticate = data.authenticate(UserName, password);
return authenticate.ToString();

PREFERRED OPTION:
Furthermore, I'm not sure why you are returning a string representation of true/false (bool)... If it were my function, I would probably return this:

首选选项:此外,我不知道你为什么要返回true / false(bool)的字符串表示...如果它是我的函数,我可能会返回:

return data.authenticate(UserName, password);

I would highly encourage you to simply return the Boolean value in the PREFERRED OPTION area. There is no obvious reason to keep this in a string format.

我强烈建议您只在“PREFERRED OPTION”区域中返回布尔值。没有明显的理由将其保留为字符串格式。

#2


Use the Boolean.ToString Method (documentation can be found here) like so:

使用Boolean.ToString方法(文档可以在这里找到),如下所示:

using System;
using System.Collections.Generic;
using System.Text;

namespace businessTMS
{
    public class SignIn
    {
        public string authenticate(String UserName, String password)
        {  
            dataTMS.SignIn data = new dataTMS.SignIn();

            return data.authenticate(UserName, password).ToString();
        }

    }
}