C# 带有非字符串返回类型的日期检查-设计
这就是我现在拥有的。但是,如果日期格式不正确,我希望有第三个选项。现在,它直接跳到else并返回false 问题是我想要三种可能的结果: true-->尚未过期 false-->已过期 第三个-->输入的日期无效 我只是被困在如何到达那里。我知道我可以很容易地使用字符串返回类型,但是有什么办法吗?因为如果成功返回bool,您可以触发它C# 带有非字符串返回类型的日期检查-设计,c#,C#,这就是我现在拥有的。但是,如果日期格式不正确,我希望有第三个选项。现在,它直接跳到else并返回false 问题是我想要三种可能的结果: true-->尚未过期 false-->已过期 第三个-->输入的日期无效 我只是被困在如何到达那里。我知道我可以很容易地使用字符串返回类型,但是有什么办法吗?因为如果成功返回bool,您可以触发它 public static bool CheckExpired() { DateTime expiryDate, currentDate = DateTi
public static bool CheckExpired()
{
DateTime expiryDate, currentDate = DateTime.Today;
DateTime.TryParse(date, out expiryDate);
if (expiryDate > currentDate)
{
return true;
}
else { return false; }
}
然后对返回类型使用Nullable而不是bool。您可以使用
NullableBool
if(DateTime.TryParse(date, out expiryDate))
{
if (expiryDate > currentDate)
{
return true;
}
else { return false; }
}
else
{
Console.Write("Invalid date.");
}
true-->尚未过期
false-->已过期
null-->输入的日期无效您可以使用可为空的bool(bool?)或int 如果无法将字符串转换为日期时间,它将返回null
public static bool? CheckExpired(string date)
{
DateTime expiryDate, currentDate = DateTime.Today;
if (DateTime.TryParse(date, out expiryDate))
{
return expiryDate > currentDate;
}
return null;
}
我稍微重构了代码。但这可能是一个解决方案:
public static bool? CheckExpired()
{
DateTime expiryDate, currentDate = DateTime.Today;
DateTime.TryParse(date, out expiryDate);
if (expiryDate == new DateTime())
{
return null;
}
if (expiryDate > currentDate)
{
return true;
}
else
{ return false; }
}
一些选择
bool?
类型;然后,您可以返回null
。问题是,“null”在您的上下文中并没有真正的意义,所以用法不清楚异常。可能会使用法更清晰(不能传递格式不正确的日期),但这意味着您需要在所有呼叫者中尝试捕获
enum
作为返回值,可以显式设置结果的名称public static bool CheckExpired()
{
DateTime expiryDate, currentDate = DateTime.Today;
if (!DateTime.TryParse(date, out expiryDate))
{
throw new Exception("Invalid date");
}
return (expiryDate > currentDate);
}
听起来像是……的工作。我会选择枚举。大致如下:
public enum ExpiredResult
{
Expired,
NotExpired,
FormatError,
}
试试这个:
public static DateValidationResult CheckExpired(string date)
{
DateTime expiryDate, currentDate = DateTime.Today;
if (!DateTime.TryParse(date, out expiryDate))
return DateValidationResult.InvalidDate;
return expiryDate > currentDate ? DateValidationResult.Ok : DateValidationResult.Fail;
}
public enum DateValidationResult
{
InvalidDate,
Ok,
Fail
}
检查null
返回值或有效的true
/false
值,如下所示:
public static bool? CheckExpired(string date)
{
DateTime expiryDate;
DateTime currentDate = DateTime.Today;
if (!DateTime.TryParse(date, out expiryDate))
{
return null;
}
return (expiryDate > currentDate);
}
就我个人而言,我只需编写一个枚举来封装返回状态,如下所示:
string date = "2/4/2013";
bool? isExpired = CheckExpired(date);
if (!isExpired.HasValue)
{
Console.Write("Invalid date");
}
else if (isExpired.Value)
{
Console.Write("Expired");
}
else // if (!isExpired.Value)
{
Console.Write("Valid");
}
我真的不喜欢这种东西的空布尔值。在调用站点,返回值应该是什么从来都不是很清楚。对于枚举,它是显式的
[编辑]按此操作。:) 你的问题标题不会反映你的问题。@Antonijn-我的目标是检查一个与当前日期相关的日期。我想以非字符串变量类型返回结果。我需要设计方面的帮助。我不知道怎样才能用更好的方式来表达我编辑了你的标题。请看,“,其中的共识是“不,他们不应该”。也考虑了这个解决方案,但它是“可理解的”吗?啊,可空类型会给我第三个选择。我懂了。谢谢@Antonijn我也可以抛出一个异常,但他的第三个选项只是说,
third-->输入的日期无效
,所以我假设他使用的是一个控制台应用程序。这并没有真正回答“如何为无效日期输入返回值”的问题@Bob标准错误输出流:Enum,我没有想到这一点。我试图避免这个任务的异常处理,所以这也很好。谢谢
public enum ExipiryStatus
{
Expired,
NotExpired,
InvalidDate
}
public static ExipiryStatus CheckExpired(string date)
{
DateTime expiryDate, currentDate = DateTime.Today;
if (DateTime.TryParse(date, out expiryDate))
{
if (expiryDate > currentDate)
{
return ExipiryStatus.Expired;
}
else
{
return ExipiryStatus.NotExpired;
}
}
else
{
return ExipiryStatus.InvalidDate;
}
}