2016-06-08 91 views
2

我有這方面的for循環:退出對於vb.net不會退出

For x = search_phone.Length To 0 Step -1 
        For m = 1 To number_call_costs 
         If search_phone.Substring(0, x) = call_costs_data(m, 2) Then 
          match = True 

          customer_cost = callcost_lookup("sequence", call_costs_data(m, 1), customer_sequence, "cost") 
          customer_connection = callcost_lookup("sequence", call_costs_data(m, 1), customer_sequence, "connection") 
          description = call_costs_data(m, 3) 
          MsgBox(call_costs_data(m, 3)) 
          Exit For 
          Exit For 
         End If 
        Next 
       Next 

我與測試的數據是:

search_phone = '101' 
call_costs_data(m, 2) = one row is '1' and another row is '101' 

其在循環,但其匹配search_phone101不退出,因爲它再次匹配到1

+1

那第二個出口將不會達到。在x循環的末尾,嘗試'If match = True Then Exit For'。 – LarsTech

+0

啊當然!衛生署!所以在第一個「Next」之後放置另一個'Exit For'? – charlie

回答

2

那第二個出口不會到達,所以試試這樣:

For x As Integer = search_phone.Length To 0 Step -1 
    match = False 
    For m As Integer = 1 To number_call_costs 
    If search_phone.Substring(0, x) = call_costs_data(m, 2) Then 
     match = True 
     '// code... 
     Exit For 
    End If 
    Next 
    If match Then 
    Exit For 
    End If 
Next 
+0

我不能只用'If match Then' ... – charlie

+0

@charlie是的,最好。 – LarsTech