且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#输入字符串的格式不正确

更新时间:2023-11-08 10:52:10

例如,如果有人在文本框中键入"foo",就会发生这种情况.您应该改用 int.TryParse ,这样一来,您就可以检测到错误而不会引发异常.

That will happen if someone has typed "foo" into the text box, for example. You should use int.TryParse instead, which lets you detect the error without an exception being thrown.

我注意到您实际上并没有使用方法的返回值-就像您没有使用 time.AddHours 的返回值一样.您知道 DateTime.AddHours 并没有真正修改您正在调用的值,不是吗?(我怀疑您实际上需要对这种方法进行一些调整,实际上,它存在各种潜在的问题,具体取决于您要尝试执行的操作.)

I notice that you're not actually using the return value of your method though - just like you're not using the return value of time.AddHours. You are aware that DateTime.AddHours doesn't actually modify the value you're calling it on, aren't you? (I suspect you'll need to tweak that method quite a bit, actually... there are various potential problems with it, depending on exact what you're trying to do.)

(最后,我将方法名称更改为 IncrementHour 以符合.NET约定.)

(Finally, I'd change the method name to IncrementHour to comply with .NET conventions.)