2012-04-18 4 views
1

어떻게 이것을 수정할 수 있습니다돌려 두 값

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

이 두 값을 반환하려면? 현재 데이터베이스에서 '상태'열을 예 또는 아니요로 반환합니다. 완료 날짜 및 상태를 어떻게 반환 할 수 있습니까?

답변

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; 
    } 
+0

정말 감사합니다. 정말 귀하의지도를 주셔서 감사합니다 –

+0

이 작품을 만들 수있었습니다, 감사합니다! –

2

제 생각에는 클래스 또는 결과의 구조체를 작성하는 가장 좋은 방법은 제 생각입니다.

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; 
    } 
+0

어떤 방법 으로든 exaqmple –

+1

을 제공해 주실 수 있습니까? 클래스/구조체 또는 out 매개 변수? – Tomtom

+0

개체를 반환 할 방법이 없으므로이를 호출하는 다른 쪽에서 할 수 있습니다. '경우 (rtnVal.Status) { lbl.text = rtnVal.CompletedDate }' –

1

작은 예입니다 아웃 매개 변수를 사용할 수 있습니다 이렇게하려면 일반 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의 두 가지 속성이 있습니다. 열쇠는 귀하의 상태가되며 값은 귀하의 날짜가 될 것입니다.

null 날짜가 필요한 경우 nullable DateTime을 사용해야합니다.

+0

새로운 매개자. 이 매개 변수는 키워드 'out'을 사용하여 함수에 제공합니다. 그것은 당신이 객체에 대한 참조를주는 것과 같습니다. 이 방법에서는 매개 변수로 작업 할 수 있습니다. 메서드를 떠난 후에 매개 변수는 새 값 –

+1

을 이해 해달라고 – Tomtom

+0

을 가지고 있습니다. 우리가 OUT을 어느 상황에서 사용하는지 말해 주시겠습니까? 왜 우리는 두 가지 속성을 가진 객체처럼 정규 코드에 "정규화"하지 않을까요? –

1

그것은 struct이와 속성을 정의하는 것이 가장이지만, 경우에 당신이 정말로 원하는하지 않습니다

그렇지 않으면 당신은 여기

+0

구조체에 대한 정보를 찾고 있는데, 매우 유망 해 보입니다.하지만 그들과 함께 연습해야합니다. 답변 감사합니다. :) –