检查日期过程时出错 [英] error when check date process
问题描述
hi
当我写下以下命令时:
hi
when i write following command:
public bool CheckeDate(MaskedTextBox txt)
{
bool ValidDate = default(bool);
int Year = 0;
int Month = 0;
int Day = 0;
System.Globalization.PersianCalendar PDate = new System.Globalization.PersianCalendar();
if (!(txt.Text == ""))
{
ValidDate = true;
if (txt.Text.Trim().Length != 10)
{
txt.BackColor = Color.MistyRose;
ValidDate = false;
}
else
{
Year = int.Parse(txt.Text.Trim().Substring(0, 4));
Month = int.Parse(txt.Text.Trim().Substring(5, 2));
Day = int.Parse(txt.Text.Trim().Substring(8, 2));
if ((txt.Text.Substring(4, 1) == "/") && (txt.Text.Substring(7, 1) == "/"))
{
if (((Day > 31 | Day < 1) || (Month > 12 | Month < 1)) || (Month > 6 & Day > 30) || (Year > 9378 | Year < 1))
{
txt.BackColor = Color.MistyRose;
ValidDate = false;
}
else
{
if (!PDate.IsLeapYear(Year, 0) && Month == 12 & Day > 29)
{
txt.BackColor = Color.MistyRose;
ValidDate = false;
}
else
{
ValidDate = true;
}
}
}
else
{
txt.BackColor = Color.MistyRose;
ValidDate = false;
}
if (!ValidDate)
{
txt.Focus();
txt.SelectAll();
MessageBox.Show("Date is not valid");
}
return ValidDate;
}
}
if (!ValidDate)
{
txt.Focus();
txt.SelectAll();
MessageBox.Show("Date is not valid");
}
}
编译后显示此错误:
错误6'myprj.form1.CheckDate(PersianDateTimePicker.PersianDateTimePicker.cmpPersianDateTimePicker)':并非所有代码路径都返回值
谢谢
this error be shown after compile :
Error 6 'myprj.form1.CheckDate(PersianDateTimePicker.PersianDateTimePicker.cmpPersianDateTimePicker)': not all code paths return a value
thanks
推荐答案
public bool CheckeDate ( )
{
bool ValidDate = default( bool );
if ( )
{
ValidDate = true;
if ( )
{
ValidDate = false;
}
else
{
if ( )
{
if ( )
{
ValidDate = false;
}
else
{
if ( )
{
ValidDate = false;
}
else
{
ValidDate = true;
}
}
}
else
{
}
if ( !ValidDate )
{
}
return ValidDate;
}
}
if ( !ValidDate )
{
}
// move return here...
}
这是你的代码的'骨架'...撕掉任何不返回相关的东西...
你可以看到你只返回值一个地方 - 在其他地方!
你的代码可能永远不会到达那个部分,在这种情况下你没有回报!
因为你已经在当地工作了变量你可以做的最好的事情就是将返回值移到方法的末尾...(参见代码中的注释)
This is the 'skeleton' of your code...ripped of anything not return related...
You can see that you do return value only at one place - inside an else!
It is possible that your code will never hit that part and in that case you have no return!
As you already working with a local variable the best thing you can do is moving the return to the end of the method...(see remark in code)
方法CheckeDate被定义为返回一个布尔值。
你必须确保所有的exceution路径返回一个布尔值;
一个更好的想法是改为使用单点返回
有多个。
l中的代码例如,如果声明,例如不返回任何内容。
The method "CheckeDate" is defined to return a boolean value.
You must make sure that all exceution paths return a boolean value;
A better idea would be to have a single point of return instead
of having multiple ones.
The code in the last if-statement e.g. does not return anything.
您的函数必须返回bool
值。但是你的功能结束时没有退货声明。
所以只需添加它:
Your function must return abool
value. But there is no return statement at the end of your function.
So just add it:
// ...
return ValidDate;
}
这篇关于检查日期过程时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!