使用非字符串返回类型进行日期检查 - 设计

时间:2013-02-04 21:02:10

标签: c#

public static bool CheckExpired()
{
    DateTime expiryDate, currentDate = DateTime.Today;
    DateTime.TryParse(date, out expiryDate);

    if (expiryDate > currentDate)
    {
        return true;
    }
    else { return false; }
}

这就是我现在所拥有的。但是,如果日期格式不正确,我希望有第三个选项。就像现在一样,它直接跳到else并返回false。

问题是我想要三个可能的结果:

true - >尚未过期

false - >已过期

第三 - >输入的日期无效

我只是坚持如何到达那里。我知道我可以很容易地使用String返回类型,但是有什么方法吗?

9 个答案:

答案 0 :(得分:6)

一些选择;

  1. 使用bool?类型;那么你可以返回null作为结果。与此相关的是,“null”在您的上下文中并没有真正意义,因此使用不明确

  2. 如果格式不正确,
  3. 抛出Exception。可能会使用法更清晰(无法通过格式错误的日期),但意味着您需要在所有来电者中使用try-catch

  4. 使用enum作为返回值,可以显式设置结果的名称。

  5. 我认为枚举可能在这里最有意义,并且对于该方法的消费者来说是最清楚的;

    public enum ExpiredResult
    {
        Expired,
        NotExpired,
        FormatError,
    }
    

答案 1 :(得分:5)

由于DateTime.TryParse()成功后会返回一个bool,你可以触发它。

if(DateTime.TryParse(date, out expiryDate))
{
    if (expiryDate > currentDate)
    {
        return true;
    }
    else { return false; }
}
else
{
    Console.Write("Invalid date.");
}

然后使用Nullable代替bool作为返回类型。

答案 2 :(得分:5)

您可以使用NullableBool

    public static bool? CheckExpired(string date)
    {
        DateTime expiryDate, currentDate = DateTime.Today;
        if (DateTime.TryParse(date, out expiryDate))
        {
            return expiryDate > currentDate;
        }
        return null;
    }

true - >尚未过期

false - >已过期

null - >输入的日期无效

答案 3 :(得分:1)

我稍微重构了一下代码。但这可能是一个解决方案:

public static bool CheckExpired()
{
    DateTime expiryDate, currentDate = DateTime.Today;

    if (!DateTime.TryParse(date, out expiryDate))
    {
         throw new Exception("Invalid date");
    }

    return (expiryDate > currentDate);
}

答案 4 :(得分:0)

您可以使用可空的bool(bool?)或int

如果字符串无法转换为日期时间,它将返回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; }
    }

答案 5 :(得分:0)

听起来像enum ...

的工作

答案 6 :(得分:0)

我会选择枚举。有点像:

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
}

答案 7 :(得分:0)

试试这个:

public static bool? CheckExpired(string date)
{
    DateTime expiryDate;
    DateTime currentDate = DateTime.Today;

    if (!DateTime.TryParse(date, out expiryDate))
    {
        return null;
    }

    return (expiryDate > currentDate);
}

检查null返回值,或有效的true / false值,如下所示:

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");
}

答案 8 :(得分:0)

就我个人而言,我只是编写一个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;
    }
}

对于这种事情,我真的不喜欢可空的布尔。它在调用网站上从未非常清楚返回值应该是什么。有了枚举,它是明确的。

[编辑]被打败了。 :)