2012-06-14 47 views
0

我使用函數將時間字符串(「hh \:mm \:ss \,fff」 - 例如:「00:00:00,100」)轉換爲零件
strTime = 「00:00:00100」=
ħ INT = 0
INT = 0
INT = 0
毫秒 INT = 100


功能:
Vb.Net我想在我的函數中修復的東西 - 秒錶

Public Function ShowInLabel(ByVal TEXT As String, ByVal time As String, ByVal startTime As Boolean) As Boolean 
     On Error Resume Next 
     Dim sss As String 
     sss = time 
     Dim start As String = StrReverse(sss) 
     start = StrReverse(start.Substring(0, 3)) 
     Dim s As Integer 
     s = Integer.Parse(start) 
     Dim secstart As String = StrReverse(sss).Substring(0, 6) 
     secstart = StrReverse(secstart) 
     Dim secs As Integer = Integer.Parse(secstart.Substring(0, 2)) 
     Dim hurs As Integer = Integer.Parse(sss.Substring(0, 2)) 
     Dim mins As Integer = Integer.Parse(StrReverse(StrReverse(sss.Substring(0, 5)).Substring(0, 2))) 

     Dim stopWatch As New Stopwatch() 
     stopWatch.Start() 
noh: 
     If stopWatch.Elapsed.Hours = hurs Then 
      GoTo yesh 
     Else 
      GoTo noh 
     End If 
yesh: 
     If stopWatch.Elapsed.Minutes = mins Then 
      GoTo yesm 
     Else 
      GoTo yesh 
     End If 
yesm: 
     If stopWatch.Elapsed.Seconds = secs Then 
      GoTo yess 
     Else 
      GoTo yesm 
     End If 
yess: 

     If stopWatch.Elapsed.Milliseconds > s Or stopWatch.Elapsed.Milliseconds = s Then 
      GoTo done 
     Else 
      GoTo yess 
     End If 
done: 
     If startTime = False Then 
      Label1.Text = "" 
     Else 
      Label1.Text = TEXT 
     End If 

     Return True 

    End Function 



例如:

ShowInLabel("SubTitle", "00:00:00,100", True) 

函數工作,
但當乳寧該應用程序的功能是Stucked 直到函數返回true
爲什麼會發生?

+4

我看到'GOTO'語句嗎? – LarsTech

+0

什麼可以替代這種說法? – nnm

+0

這會發生在Windows窗體開發?在這種情況下,Timer類可能會有所幫助。它具有Tick事件,可以解決懸掛線程問題,並幫助丟棄goto。 – Jonas

回答

1

所有你需要做的是這樣的:

Dim time As Date = DateTime.ParseExact("00:01:02,123", "hh:mm:ss,fff", CultureInfo.InvariantCulture) 
    Dim h As Integer = time.Hour 
    Dim m As Integer = time.Minute 
    Dim sec As Integer = time.Second 
    Dim millisec As Integer = time.Millisecond 

然而,它是所有熟悉你要完成什麼:),我懷疑你真正需要的是這樣的:

Dim time As Date = DateTime.ParseExact("00:01:02,123", "hh:mm:ss,fff", CultureInfo.InvariantCulture) 
    Dim startTime As Date = DateTime.ParseExact("00:00:00,000", "hh:mm:ss,fff", CultureInfo.InvariantCulture) 
    Dim elapsed As TimeSpan = time - startTime 
    Dim totalMilliseconds As Integer = CType(elapsed.TotalMilliseconds, Integer) 

您可以用同樣的方法將每個字幕的開始和結束時間轉換爲總毫秒數,然後以這種方式進行比較。

正如其他人所指出的,On Error Resume Next只在VB.NET中真正可用,以便向後兼容VB6代碼。您應該使用Try/Catch塊,而不是。但是,將簡歷放在整個方法的上方,即使在VB6中也不會被認爲是好的做法,就像在整個方法中放置try/catch塊一樣,這也是一個壞主意。

同樣,GoTo幾乎是任何程序員的敏感性所能做到的最可怕的事情。您應該考慮其他選項,例如循環,if/else塊,將代碼分解爲單獨的方法等,並且不惜一切代價避免GoTo。

+0

Tnx Bro!在這爲什麼更容易提取evreything :),但「goto」的東西仍然煩人:/ – nnm

+0

感謝您試圖在您的問題更具體。你在改進! –

+0

我tryDo雖然stopWatch.Elapsed.Seconds <> secs Application.Doevents()循環和計時器,它仍然無法工作兄弟 – nnm

相關問題