2017-03-10 1 views
-4

이미 시간 지정 또는 시간 경과 값을 재귀 적으로 추가하려면 어떻게합니까?이전 datetime 값에 datetime 값 추가

나는이 시도했지만 그것은 나에게 오류를 제공합니다

'A local variable named 'total' cannot be declared in this scope because it would give a different meaning to 'total, which is already in used in a 'parent or current' scope to denote something else'

private void button5_Click(object sender, EventArgs e) 
     { 
      int a = 0; 
      string path = @"C:\Users\Public\WriteLines.txt"; 
      using (StreamReader sr = new StreamReader(path)) 
      { 
       string line; 
       string[] lines = new String[500]; 

       DateTime now = DateTime.Now; 
       TimeSpan total = now - now; 
       int temp=0; 

       while ((line = sr.ReadLine()) != null) 
       { 
        lines[a] = line; 
        a++; 

       } 

       while (temp < a) 
       { 

        TimeSpan difference = Convert.ToDateTime(lines[temp+1]) - Convert.ToDateTime(lines[temp]); 
        TimeSpan total = total + difference; // <----ERROR HERE 
        Console.WriteLine(total); 
        Console.WriteLine(difference); 
        temp = temp + 2; 
       } 

      } 
     } 

또한 내가 재귀 적으로 값을 추가 할 수 있도록 제로로 날짜 값을 설정하는 더 좋은 방법이?

는 이미 다른 변수 이름으로 대체, 같은 이름의 변수가 있기 때문에 당신은 할 수 없어

답변

0

,

TimeSpan newTotal = total + difference; 
Console.WriteLine(newTotal); 

더 좋은 방법은 반복 할 필요가 없습니다 총하기 전에 시간 범위를 제거하는 것입니다 ,

total = total + difference; 
    Console.WriteLine(total); 
3

귀하의 방법에서 '총'을 두 번 정의하셨습니다. 두 번째 정의에서 'TimeSpan'을 제거해 보셨습니까?

0

오류가 표시되면 변수가 이미 정의되어 있습니다. 같은 변수에 작업 또는 다른 이름으로 새로운 선언

total = total + difference; 

또는

TimeSpan total2 = total + difference;