我该如何修改:
if ( reader.GetString( reader.GetOrdinal( "Status" ) ) == "Yes" )
{
return true; // Return Status + Date it was Completed
}
else
{
return false; // Return Status + null Date.
}
要返回两个值吗?目前,它从数据库中返回“状态”列,其值为“是”或“否”。如何让它返回完成的日期和状态?
答案 0 :(得分:3)
private void DoSomething() {
string input = "test";
string secondValue = "oldSecondValue";
string thirdValue = "another old value";
string value = Get2Values(input, out secondValue, out thirdValue);
//Now value is equal to the input and secondValue is "Hello World"
//The thirdValue is "Hello universe"
}
public string Get2Values(string input, out string secondValue, out string thirdValue) {
//The out-parameters must be set befor the method is left
secondValue = "Hello World";
thirdValue = "Hello universe";
return input;
}
答案 1 :(得分:2)
在我看来,这是最好的方法,为结果写一个类或结构。
否则你可以使用out-parameter
答案 2 :(得分:1)
这是一个小例子:
private void DoSomething() {
string input = "test";
string secondValue = "oldSecondValue";
string value = Get2Values(input, out secondValue);
//Now value is equal to the input and secondValue is "Hello World"
}
public string Get2Values(string input, out string secondValue) {
//The out-parameter must be set befor the method is left
secondValue = "Hello World";
return input;
}
答案 3 :(得分:1)
最好定义一个带有两个属性的struct
,但如果你真的不想这样做,你可以使用通用的KeyValuePair<TKey,TValue>
结构:
KeyValuePair<bool, DateTime?> result;
if (reader.GetString(reader.GetOrdinal("Status")) == "Yes")
{
result = new KeyValuePair<bool, DateTime?>
(true, DateTime.Now); // but put your date here
}
else
{
result = new KeyValuePair<bool, DateTime?>
(false, null);
}
// reader.Close()?
return result;
KeyValuePair
有两个属性Key
和Value
。钥匙将是您的状态,价值将是您的日期。
注意,如果您需要空日期,则需要使用nullable DateTime。