1

これを変更するにはどうすればよいですか:

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

2 つの値を返すには? 現在、データベースから列「ステータス」を「はい」または「いいえ」の値で返します。完了した日付とステータスを返すにはどうすればよいですか?

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

2 つのプロパティでa を定義するのがおそらく最善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には と の 2 つのプロパティがKeyありValueます。Key はあなたのステータス、Value はあなたの日付になります。

null の日付が必要な場合は、null可能な DateTimeを使用する必要があることに注意してください。

于 2012-04-18T20:19:31.120 に答える