是否可以从布尔方法获取字符串输出? [英] is it possible to get a string output from a boolean method?
本文介绍了是否可以从布尔方法获取字符串输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要从布尔方法输出字符串.
对于肯定的情况,我将返回true,但是对于否定的情况,我需要发送一条仅是字符串的错误消息.
在C语言中有可能吗?
请回复,我需要您的帮助.
I want string output from a boolean method.
For positive cases I''m returning true, but for negative case I need to send one error message which is string only.
Is that possible in C sharp?
Please respond, I want your help.
推荐答案
以下是您的选择:
1)使用输出参数 [
Here are your options:
1) Use out parameter[^] to pass the error message. So the message is available to the calling function.
2) ReturnString
from your method instead ofBoolean
. So, in the calling function, this will be the case:
String valid = someMethod(); //returns true or error code
if (valid == "true") //the quotes are important
{
//do something
}
else
{
//you have the error message stored in valid
}
希望这会有所帮助!
Hope this helps!
示例:
Example:
public bool SomeBooleanFunction(out String errorMessage)
{
errorMessage = "";
if(checkSomething())
{
return true;
}
else
{
errorMessage = "Something went awfully wrong: XXXX";
return false;
}
}
String msg;
bool test = SomeBooleanFunction(out msg);
if(!test)
Console.WriteLine(msg);
干杯
曼弗雷德(Manfred)
Cheers
Manfred
这是您的意思吗?
Is this what you mean?
class Program
{
static void Main(string[] args)
{
Program myProgram = new Program();
string strMessage = "";
strMessage = Convert.ToString(myProgram.BoolFunction());
Console.WriteLine(strMessage);
Console.ReadKey();
}
public bool BoolFunction()
{
return true;
}
}
这篇关于是否可以从布尔方法获取字符串输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文