从类的成员返回字符串和bool类型 [英] returning a string and bool type from a member of a class
问题描述
嗨我有以下方法返回bool,true或false取决于
on如果转换为date tiem有效。它需要一个字符串输入。我是
只返回bool但是还要返回字符串消息
ex.message,只是想知道如何做到这一点因为我认为你只能返回
功能一件事。谢谢。
public bool checkdate(String s_date)
{
b_convert = true;
DateTime dt_temp1 = Convert.ToDateTime(" 1/1/208");
try
{
dt_temp1 = Convert.ToDateTime(s_date );
}
catch(例外情况)
{
errormsg = ex.Message;
b_convert = false;
}
返回b_convert;
}
-
Paul G
软件工程师。
Hi I have the method below that returns a bool, true or false depending
on if the conversion to date tiem works. It takes a string input. I am
only returning the bool but would also like to return the string message
ex.message, just wondering how to do this since I think you can only return
one thing with the function. Thanks.
public bool checkdate(String s_date)
{
b_convert = true ;
DateTime dt_temp1 = Convert.ToDateTime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateTime(s_date);
}
catch (Exception ex)
{
errormsg = ex.Message;
b_convert = false;
}
return b_convert;
}
--
Paul G
Software engineer.
推荐答案
5月11日下午4:51,保罗< P ... @ discussion.microsoft.comwrote:
On May 11, 4:51 pm, Paul <P...@discussions.microsoft.comwrote:
您好我有以下方法返回bool,true或false取决于
$ b如果转换为date tiem有效,则为$ b。它需要一个字符串输入。我是
只返回bool但是还要返回字符串消息
ex.message,只是想知道如何做到这一点因为我认为你只能返回
功能一件事。
Hi I have the method below that returns a bool, true or false depending
on if the conversion to date tiem works. It takes a string input. I am
only returning the bool but would also like to return the string message
ex.message, just wondering how to do this since I think you can only return
one thing with the function.
你需要使用out参数:
public bool checkdate(字符串输入,输出字符串消息)
{
message = null;
...
catch(Exception ex)
{
message = ex.Message;
b_convert = false;
}
...
}
Jon
You''ll need to use an out parameter:
public bool checkdate(string input, out string message)
{
message = null;
...
catch (Exception ex)
{
message = ex.Message;
b_convert = false;
}
...
}
Jon
星期五,2007年5月11日08:51 :02 -0700,Paul< Pa ** @ discussion.microsoft.com
写道:
On Fri, 11 May 2007 08:51:02 -0700, Paul <Pa**@discussions.microsoft.com
wrote:
您好我有以下方法如果转换为date tiem有效,则返回bool,true或false,具体取决于
。它需要一个字符串输入。我是
只返回bool但是还要返回字符串消息
ex.message,只是想知道如何做到这一点因为我认为你只能
返回一个函数。谢谢。
Hi I have the method below that returns a bool, true or false depending
on if the conversion to date tiem works. It takes a string input. I am
only returning the bool but would also like to return the string message
ex.message, just wondering how to do this since I think you can only
return one thing with the function. Thanks.
您可以使用带有out的参数关键字允许额外的
信息返回:
public bool checkdate(Strings s_date,out String s_error)
{
b_convert = true;
s_error ="" ;;
DateTime dt_temp1 = Convert.ToDateTime(" 1/1) / 1980");
尝试
{
dt_temp1 = Convert.ToDateTime(s_date);
}
catch(exception ex)
{
s_error = ex.Message;
b_convert = false;
}
返回b_convert;
}
还有很多其他方法可以做到这一点,但是上面几乎是什么
" out"关键字是为了。
Pete
You can use a parameter with the "out" keyword to allow additional
information to be return:
public bool checkdate(Strings s_date, out String s_error)
{
b_convert = true ;
s_error = "";
DateTime dt_temp1 = Convert.ToDateTime("1/1/1980");
try
{
dt_temp1 = Convert.ToDateTime(s_date);
}
catch (Exception ex)
{
s_error = ex.Message;
b_convert = false;
}
return b_convert;
}
There are lots of other ways to do this, but the above is pretty much what
the "out" keyword is for.
Pete
您好,感谢您的回复,您是否使用了返回语句
变量或只有bool?
我收到一个错误(必须先分配out参数消息
控制离开当前方法)这是我有什么
公共bool checkdate(字符串输入,输出字符串消息)
{
message = null;
...
catch(exception ex)
{
message = ex.Message;
b_convert =假;
}
返回b_convert;
...
}
-
Paul G
软件工程师。
" Jon Skeet [C#MVP]"写道:
Hi thanks for the response, do you use a return statement for the out
variable or only the bool ?
I am getting an error (the out parameter message must be assigned to before
control leaves the current method) Here is what I have
public bool checkdate(string input, out string message)
{
message = null;
...
catch (Exception ex)
{
message = ex.Message;
b_convert = false;
}
return b_convert;
...
}
--
Paul G
Software engineer.
"Jon Skeet [C# MVP]" wrote:
5月11日下午4:51,Paul< P ... @ discussion.microsoft.comwrote:
On May 11, 4:51 pm, Paul <P...@discussions.microsoft.comwrote:
嗨我有以下方法返回bool,true或false取决于
如果转换为date tiem有效。它需要一个字符串输入。我是
只返回bool但是还要返回字符串消息
ex.message,只是想知道如何做到这一点因为我认为你只能返回
功能一件事。
Hi I have the method below that returns a bool, true or false depending
on if the conversion to date tiem works. It takes a string input. I am
only returning the bool but would also like to return the string message
ex.message, just wondering how to do this since I think you can only return
one thing with the function.
你需要使用out参数:
public bool checkdate(string input,out string message)
{
message = null;
...
catch(Exception ex)
{
message = ex.Message;
b_convert = false;
}
...
}
Jon
You''ll need to use an out parameter:
public bool checkdate(string input, out string message)
{
message = null;
...
catch (Exception ex)
{
message = ex.Message;
b_convert = false;
}
...
}
Jon
这篇关于从类的成员返回字符串和bool类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!