Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我会收到这样的消息:无法转换类型';布尔';至';字符串';_C#_.net_Casting_Compiler Errors - Fatal编程技术网

C# 为什么我会收到这样的消息:无法转换类型';布尔';至';字符串';

C# 为什么我会收到这样的消息:无法转换类型';布尔';至';字符串';,c#,.net,casting,compiler-errors,C#,.net,Casting,Compiler Errors,下面是我正在使用的代码片段 using System; using System.Collections.Generic; using System.Text; namespace businessTMS { public class SignIn { public string authenticate(String UserName, String password) { dataTMS.SignIn data =

下面是我正在使用的代码片段

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;
        }

    }
}

您的错误是由以下行引起的:

string authenticate = (string)data.authenticate(UserName, password);
您正在将authenticate设置为返回布尔值的真/假计算。试试这个

string authenticate = data.authenticate(UserName, password).ToString();
通过执行以下操作,还可以修改代码以仍然返回字符串:

bool authenticate = data.authenticate(UserName, password);
return authenticate.ToString();
首选选项:
此外,我不确定您为什么返回true/false(bool)的字符串表示形式。。。如果它是我的函数,我可能会返回以下内容:

return data.authenticate(UserName, password);

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

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

我收到了错误消息“无法将类型'bool'转换为'string'”,但代码中出现错误的那一行不是消息的原因


我无意中切掉了前一行函数调用的最后一个参数,它应该是bool,而不是关闭括号。

这是一个很好的代码片段,但是您的问题是什么,您到目前为止尝试了什么?您的dataTMS.SignIn authenticate方法肯定会返回布尔值。您在businessTMS中返回字符串的原因是什么?如果是这样的话,您试图将哪个字符串推回到UI?“已登录”与“未登录”?为什么用绳子?而且不仅仅是将其作为布尔值传递(public bool authenticate(…)?@Simucal:good re tag,我也正准备这么做!虽然这是一个编译器错误,而不是一个例外。
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();
        }

    }
}