1

我该如何修改这个:

if ( reader.GetString( reader.GetOrdinal( "Status" ) ) == "Yes" )
{
    return true; // Return Status + Date it was Completed
}
else
{
    return false; // Return Status + null Date.
}

返回两个值?目前它从数据库返回“状态”列,值为“是”或“否”。我怎样才能让它返回完成日期和状态?

4

4 回答 4

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;
    }
于 2012-04-18T18:08:35.573 回答
2

在我看来,最好的方法是为结果编写一个类或一个结构。

否则你可以使用 out-parameter

于 2012-04-18T17:48:20.373 回答
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;
    }
于 2012-04-18T17:56:49.607 回答
1

最好struct用两个属性定义 a ,但如果你真的不想这样做,你可以使用通用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有两个性质,KeyValue。Key 将是您的状态,Value 将是您的日期。

请注意,如果您需要空日期,则需要使用可为空的 DateTime

于 2012-04-18T20:19:31.120 回答