저는 C#과 프로그래밍의 초보자입니다. 몇 가지 DateTime
변수를 계산하려고합니다. 첫번째는 dDate
및 제 dDate1
(dDate
전날) 제 dDate2
(dDate
번째 전날, 즉 dDate1
전날) dDate
의 제 dDate3
(제 전날, 즉이라고 두 번째 전날은 dDate1
이고 전날은 dDate2
입니다. 휴일이나 주말이 아니어야합니다!주어진 DateTime 키가 사전에 없습니다.
나는 모든 휴일과 주말을 nd<DateTime, string>
이라는 사전에 저장했습니다. DateTime
키의 날짜는 2011-01-01
에서 2013-01-01
까지 1 일 단위이고 값은 string
이며 TR
또는 NT
입니다. 문자열 변수이지만 부울은 아닙니다. 주말이나 공휴일 인 경우 문자열은 NT
이며 그렇지 않은 경우 TR
입니다.
내가하려는 것은 dDate
이 주말 또는 공휴일 인 경우 하루를 뺀 것입니다. 예를 들어, dDate
는 2012-01-01
에 dDate
을 변경, 휴일이며, 주말 (일요일)이기 때문에, 2011-12-31
로 변경하고, 다시 주말, 2011-12-30
에 dDate
을 변경하는 2012-01-02
이다. dDate1
, dDate2
및 dDate3
과 동일합니다.
문제는 내 코드가 dDate
에 적합합니다. 내가 dDate1
, dDate2
또는 dDate3
을 위해 같은 일을하고 있어요
the given key was not present in the dictionary
:하지만 오류가 있습니다. 이 코드는 아래에 첨부되어 있습니다 :
private Dictionary<DateTime, string> noDates;
...
noDates = new Dictionary<DateTime, string>();
public void ImportNoDate()
{
string str;
string[] line = new string[0];
while ((str = reader.ReadLine()) != null)
{
line = str.Split(',');
String date = line[1];
String flag = line[2];//flag is "NT" or "TR"
String[] tmp = date.Split('-');
date = Convert.ToInt32(tmp[0]) + "-" + Convert.ToInt32(tmp[1]) + "-" + Convert.ToInt32(tmp[2]);
DateTime noDate = DateTime.Parse(date);
noDates.Add(noDate, flag);
}
}
public void ImportdDate()
{
...
DDates dd = new DDates(dDate, noDates); //dDate is defined similar to noDate, it is just another //series of date
}
//DDates is an auxiliary cs file called DDates.cs
public DDates(DateTime dd, Dictionary<DateTime, string> nd)
{
dDate1 = dDate.AddDays(-1);
dDate1 = dDate.AddDays(-2);
dDate3 = dDate.AddDays(-3);
// dDate is imported from data file and has been Parse
// to DateTime and it is something like
// 2012-01-01 12:00:00 AM
if (nd.ContainsKey(dDate))
{
while (nd[dDate].Contains("NT"))
{
dDate = dDate.AddDays(-1);
}
}
//It works fine till here:
if (nd.ContainsKey(dDate1))
{
//It gives "the given key was not present in the dictionary" here:
while (nd[dDate1].Contains("NT"))
{
dDate1 = dDate1.AddDays(-1);
}
}
}
여러 스레드를 사용하고 있습니까? –
사전을 채우는 방법을 보여줘야합니다. –
은 여러분의 모든 코드이거나, inbetween 부분이없는 부분입니다 –