2013-03-14 1 views
1

바보 같은 느낌이 드는 이유는 뭔가 쉽게 누락되었거나 tryParse 함수를 전혀 이해하지 못하기 때문입니다.int.TryParse 결과의 값 비교

laymans 용어로, 다음 코드에서 수행하려는 작업은 DataGridview의 모든 열을 통해 코드 루프를 수행하는 것입니다. 열 이름의 마지막 2 문자가 숫자 인 경우, 마지막 2 문자 (int.TryParse 사용)의 숫자 값을 다른 변수와 비교하려고합니다. 근본적으로 나는 모든 컬럼을 오직 마지막 2 자리가 정수로 변환 될 수 있고 그 정수가 내가 비교할 수있는 것보다 더 큰 칼럼에 대해서만 excpet를 읽길 원합니다.

아래 코드는 int_tryParse 전에 단계 설정 변수를 단계별로 설정하려고 시도 했으므로 아마 약간 평범 할 것입니다. 나중에 수정할 수 있습니다.

C#을, VS2008 :

foreach (DataGridViewColumn col in grd1.Columns) 
       { 
        string myCol = col.Name; 
        int myColLength = col.Name.Length; 
        string myColMonth = myCol.Substring(myColLength - 2); 
        if (int.TryParse(myColMonth, out myColMonth) <= myMostRecentActualMonth) 
        { 
         col.ReadOnly = true; 
        } 
        else 
        { 
         col.ReadOnly = false; 
        } 
       }   

답변

2

TryParse 방법은 구문 분석이 성공인지 아닌지를 나타내는 Boolean 값을 반환합니다. 그렇다면 출력 매개 변수를 구문 분석 된 값으로 설정합니다.

것은 여기에서하려고 시도해보십시오 : 당신은 당신의 코드를 다시 작성할 수

 foreach (DataGridViewColumn col in grd1.Columns) 
     { 
      string myCol = col.Name; 
      int myColLength = col.Name.Length; 
      string myColMonth = myCol.Substring(myColLength - 2); 
      int myIntColMonth; 
      if (int.TryParse(myColMonth, out myIntColMonth) 
       && myIntColMonth <= myMostRecentActualMonth) 
      { 
       col.ReadOnly = true; 
      } 
      else 
      { 
       col.ReadOnly = false; 
      } 
     } 
+0

감사 마이크. StackOverflow에가 있기 때문입니다에 내가 질문을 게시 왜 사실 큰 이유 - 당신은 내가 (한숨)를 게시 한 후 나는 10 분을 알아 낸 것이 작업의 40분 –

+0

@RyanWard 후 물론 알고 있기 때문에 기본적으로 나는 비슷한 솔루션을 함께했다 그것은 나 자신의 30 초 후에 그것을 이해할 것을 보증한다. –

0

int.TryParse이 부울의 반환 형식이 있습니다

은 그래서 당신이 원하는 것은 무엇인가 같다 이렇게하려면 먼저 구문 분석 된 값을 저장해야하는 다른 int 값이 필요하다면 해당 값이 myMostRecentActualMonth보다 낮은 지 확인하십시오

foreach (DataGridViewColumn col in grd1.Columns) 
{ 
    string myCol = col.Name; 
    int myColLength = col.Name.Length; 
    string myColMonth = myCol.Substring(myColLength - 2); 
    int myColMonthIntValue = int.MaxValue; 
    if (int.TryParse(myColMonth, out myColMonthIntValue) && myColMonthIntValue <= myMostRecentActualMonth) 
    { 
     col.ReadOnly = true; 
    } 
    else 
    { 
     col.ReadOnly = false; 
    } 
} 
0

int parsedMonth; // This will get set if myColMonth is a valid integer 
if (int.TryParse(myColMonth, out parsedMonth) && parsedMonth <= myMostRecentActualMonth) 
{ 
    // ... 
} 
0

TryParse은 변환이 성공했는지 여부를 나타내는 bool을 반환합니다. 당신은 TryParse의 결과 (당신이 무슨 일을하는지) 대신 당신이 그것으로 전달하는 변수와 비교할를 비교하고 싶지 않아요.

그래서; (myColMonth 아웃 int.TryParse (myColMonth) < = myMostRecentActualMonth)

요구 될 경우;

if (int.TryParse(myColMonth, out myColMonth) 
    if (myColMonth <= myMostRecentActualMonth) 

먼저 int를 구문 분석 한 다음 비교를 수행하는지 확인하십시오.

0
foreach (DataGridViewColumn col in grd1.Columns) 
    { 
     string myCol = col.Name; 
     int myColLength = col.Name.Length; 
     string myColMonth = myCol.Substring(myColLength - 2); 
     int myColMonthInt = 0; 
     if (int.TryParse(myColMonth, out myColMonthInt)) 
     { 
      if (myColMonthInt <= myMostRecentActualMonth) 
      { 
       col.ReadOnly = true; 
      } 
      else 
      { 
       col.ReadOnly = false; 
      } 
     } 
     else 
     { 
      // what do you want to do is last two chars can't be converted to int? 
      col.ReadOnly = true; 
     } 
    }