如何將遞歸時間添加到timedate或時間跨度值?將日期時間值添加到以前的日期時間值
我已經試過這一點,但它給了我錯誤:
'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;
}
}
}
也就是有沒有更好的辦法,以日期時間值設置爲零,這樣我可以遞歸增加值?