Timespan不能Convert.ToInt16(elapsedSeconds))并且不能Convert.ToInt64(elapsedSeconds))错误Unity C#

时间:2016-07-16 17:37:39

标签: c# unity3d

尝试将变量elapseseconds转换为ToInt16和ToInt64时出错。

TimeSpan cTime = new TimeSpan (0, 0, 0, Convert.ToInt16(elapsedSeconds));

出现错误:

OverflowException: Value is greater than Int16.MaxValue or less than Int16.MinValue
System.Convert.ToInt16 (Int64 value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:1093)
margaretSellTimer.OnResumeSession () (at Assets/script/margaretShop/margaretSellTimer.cs:148)
margaretSellTimer.Awake () (at Assets/script/margaretShop/margaretSellTimer.cs:28)

而且我也尝试将The line更改为:

TimeSpan cTime = new TimeSpan (0, 0, 0, Convert.ToInt64(elapsedSeconds));

这也有错误:

Assets/script/margaretShop/margaretSellTimer.cs(148,89): error CS1502: The best overloaded method match for `System.TimeSpan.TimeSpan(int, int, int, int)' has some invalid arguments

Assets/script/margaretShop/margaretSellTimer.cs(148,89): error CS1503: Argument `#4' cannot convert `long' expression to type `int'

我看到的是elapsedSeconds是大值,所以我尝试使用ToInt64(长),但不能像那样转换..

实际上我想从cTime中得到的内容如下:

Seconds = cTime.Seconds;
            Minutes = cTime.Minutes;
            Hours = cTime.Hours;
            days = cTime.Days;

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:3)

如果您只需要从几秒钟构建TimeSpan,就可以使用它:

TimeSpan cTime = TimeSpan.FromSeconds(Convert.ToDouble(elapsedSeconds));