2016-07-16 40 views
0

當我嘗試將變量elapseseconds轉換爲ToInt16和ToInt64時出現錯誤。Timespan不能Convert.ToInt16(elapsedSeconds))並且不能Convert.ToInt64(elapsedSeconds))錯誤Unity C#

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) 

,也是我嘗試改變線路是這樣的:

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是對大v ALUE,所以我儘量用ToInt64(長),但不能像被轉換..

其實我從CTIME想是象下面這樣:

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

任何想法?

感謝

+0

什麼類型'elapsedSeconds'和價值持有呢? – lokusking

+0

它很長elapsedSeconds = Convert.ToInt64(timestamp) - oldTimestamp; –

回答

3

你可以使用這個,如果你只是需要建立從秒時間跨度:

TimeSpan cTime = TimeSpan.FromSeconds(Convert.ToDouble(elapsedSeconds)); 
相關問題