English 中文(简体)
• 如何使时间适当。 TryParse()
原标题:How to make this to a proper DateTime.TryParse()
  • 时间:2011-03-01 14:48:14
  •  标签:
  • c#

我怎么会把这一点变成一个尝试?

private static DateTime _endDate = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["EndDate"])

private static DateTime _endDate = DateTime.TryParse(System.Configuration.ConfigurationManager.AppSettings["EndDate"], out Datetime _endDate)
最佳回答

将:

private static DateTime _endDate;
if (!DateTime.TryParse(System.Configuration.ConfigurationManager.AppSettings["EndDate"], out _endDate)) {
    // handle failure
}

当然,<代码>><>if/code>部分必须放在您的构造中,在班级申报中可能不会松散。

问题回答

无,因为<代码>TryParse 回归代码

如果教区失败,你需要思考你想要做些什么。 例如,你可能想要违约,在这种情况下,你可以撰写一种方法,例如:

public static DateTime ParseWithDefault(string text, DateTime defaultValue)
{
    DateTime ret;
    if (!DateTime.TryParse(text, out ret))
    {
        ret = defaultValue;
    }
    return ret;
}

页: 1 如果你想要这样做的话,你需要做你想要做的事,但变式的初始器必须作为“条码”的表示(或默示可兑换)。

如果你需要更复杂的逻辑,你可能为此使用固定构造。

var date = "01/01/2001";

DateTime result;

if (DateTime.TryParse(date, out result))
{
    // it worked
}

Give that a shot..

日期: TryParse回到了一种ool子,因此不能给它分配一个时间。 你们应该首先宣布,然后把成功分配给一个ool。

private static DateTime _endDate;
private bool success = _endDate.TryParse(System.Configuration.ConfigurationManager.AppSettings["EndDate"], out _endDate);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签