您好我创建一个酒店预订表格,并想计算住宿的总住宿费用。它需要到达日期和出发日期,但我想添加验证,因此如果用户输入的格式不正确,则会显示一个消息框,要求他们再次尝试。这里是我的代码已经有一些帮助转换时间跨度所以任何帮助将是惊人的。该错误出现在以“dateDiff = aDate”开头的行上,它表示变量aDate和dDate未提前感谢:
String arrival, departure;
arrival = textBox1.Text;
departure = textBox2.Text;
DateTime aDate, dDate;
try
{
aDate = DateTime.ParseExact(arrival, "dd/mm/yyyy", null);
dDate = DateTime.ParseExact(departure, "dd/mm/yyyy", null);
return;
}
catch
{
MessageBox.Show("Invalid input format please enter in format DD/MM/YYYY");
}
TimeSpan dateDiff;
dateDiff = dDate.Subtract(aDate);
int nights = (int)dateDiff.TotalDays;
textBox3.Text = ("" + nights);
textBox5.Text = ("£" + (nights * 115));
答案 0 :(得分:2)
编译器警告的原因是您尚未为本地DateTime
字段分配值。本地变量未使用默认值初始化,因此必须先手动执行,然后才能使用它们。由于您在Try/Catch
中分配了值,因此无法确保它们会获得一个值。
相反,您可以使用DateTime.TryParseExact
:
DateTime aDate, dDate;
if( DateTime.TryParseExact(arrival, "dd/mm/yyyy", null, DateTimeStyles.None, out aDate)
&& DateTime.TryParseExact(departure, "dd/mm/yyyy", null, DateTimeStyles.None, out dDate))
{
// ...
}
else{
MessageBox.Show("Invalid input format please enter in format DD/MM/YYYY");
}
答案 1 :(得分:1)
你的代码在你的捕获后继续。使用try-block中的日期放置代码。
String arrival, departure;
arrival = textBox1.Text;
departure = textBox2.Text;
DateTime aDate, dDate;
try
{
aDate = DateTime.ParseExact(arrival, "dd/mm/yyyy", null);
dDate = DateTime.ParseExact(departure, "dd/mm/yyyy", null);
TimeSpan dateDiff;
dateDiff = dDate.Subtract(aDate);
int nights = (int)dateDiff.TotalDays;
textBox3.Text = ("" + nights);
textBox5.Text = ("£" + (nights * 115));
}
catch
{
MessageBox.Show("Invalid input format please enter in format DD/MM/YYYY");
}
如果他们成功解析,则不会返回,或者当您的输入 验证时,您将无法获得结果。
或者,将return放在catch块中,以便在失败时停止执行。
答案 2 :(得分:1)
您的代码应为
String arrival, departure;
arrival = textBox1.Text;
departure = textBox2.Text;
DateTime aDate, dDate;
try
{
aDate = DateTime.ParseExact(arrival, "dd/mm/yyyy", null);
dDate = DateTime.ParseExact(departure, "dd/mm/yyyy", null);
TimeSpan dateDiff;
dateDiff = dDate.Subtract(aDate);
int nights = (int)dateDiff.TotalDays;
textBox3.Text = ("" + nights);
textBox5.Text = ("£" + (nights * 115));
}
catch
{
MessageBox.Show("Invalid input format please enter in format DD/MM/YYYY");
return;
}
答案 3 :(得分:0)
为aDate
,dDate
分配一些默认值,错误就会消失。原因是编译器无法确定是否在try块中为它们确定了值。你可以做到
DateTime aDate = default(DateTime);
DateTime dDate = default(DateTime);
<强> BUT 强>
而不是使用try-catch来验证日期,如果你使用DateTime.TryParseExact
,它会更好DateTime aDate, dDate;
if (DateTime.TryParseExact(arrival,
"dd/MM/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.NoCurrentDateDefault,
out aDate))
{
MessageBox.Show("Invalid input format please enter in format DD/MM/YYYY");
}
所以你的完整代码应该是:
String arrival, departure;
arrival = textBox1.Text;
departure = textBox2.Text;
DateTime aDate, dDate;
if (DateTime.TryParseExact(arrival,
"dd/MM/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.NoCurrentDateDefault,
out aDate))
{
MessageBox.Show("Invalid input format for Arrival Date - please enter in format DD/MM/YYYY");
}
if (DateTime.TryParseExact(departure,
"dd/MM/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.NoCurrentDateDefault,
out dDate))
{
MessageBox.Show("Invalid input format for Departure Date - please enter in format DD/MM/YYYY");
}
TimeSpan dateDiff;
dateDiff = dDate.Subtract(aDate);
int nights = (int)dateDiff.TotalDays;
textBox3.Text = ("" + nights);
textBox5.Text = ("£" + (nights * 115));
答案 4 :(得分:0)
使用不需要解析的dateTimePicker控件