2015-10-29 87 views
-1

我有這段代碼,我希望這個循環爲每一行迭代。但是,如果if條件爲false,則此代碼不會迭代。
我在其他部分嘗試過「繼續」,但它沒有奏效。即使如果我將msgbox放入其他部分並在每次顯示時單擊msg框,循環都會繼續。但它不實用,因爲我有1000行要檢查。For循環If條件 - 當條件不滿足時迭代

Public Function SMS() 
    CustomerTableAdapter.Fill(MyHotelManagementSystemDataSet63.Customer) 
    For i = 0 To MyHotelManagementSystemDataSet63.Customer.Rows.Count - 1 
     If MyHotelManagementSystemDataSet63.Customer(i).DOB.Day = Date.Now.Day AndAlso _ 
      MyHotelManagementSystemDataSet63.Customer(i).DOB.Date.Month = Date.Now.Month Then 
      Dim SerialPort As New System.IO.Ports.SerialPort() 
      If SerialPort.IsOpen Then 
       SerialPort.Close() 
      End If 
      SerialPort.PortName = "COM29" 
      SerialPort.BaudRate = 9600 
      SerialPort.Parity = Parity.None 
      SerialPort.StopBits = StopBits.One 
      SerialPort.DataBits = 8 
      SerialPort.Handshake = Handshake.RequestToSend 
      SerialPort.DtrEnable = True 
      SerialPort.RtsEnable = True 
      SerialPort.NewLine = vbCrLf 
      Dim message As String 
      Dim nm = MyHotelManagementSystemDataSet63.Customer(i).Name 
      Dim tp = MyHotelManagementSystemDataSet63.Customer(i).Telephone 
      message = "Dear " + nm + " ***)" 
      SerialPort.Open() 
      If SerialPort.IsOpen() Then 
       SerialPort.Write("AT" & vbCrLf) 
       SerialPort.Write("AT+CMGF=1" & vbCrLf) 
       SerialPort.Write("AT+CMGS=" & Chr(34) & tp & Chr(34) & vbCrLf) 
       SerialPort.Write(message & Chr(26)) 
       SerialPort.Close() 
      Else 
       MsgBox("Port not available") 
      End If 
     Else 

     End If 
    Next 
    Return True 
End Function 

Pleaes幫我做這個for循環繼續,不需要任何用戶交互。

+2

您的問題不清楚。你說「不要交際」是什麼意思?爲什麼? – DarkKnight

+0

實際上,如果'i> = 2',循環會自動持續。你不需要明確地調用「繼續」。 –

+0

循環從'0到CustomerDataSet.customer.Rows.count - 1'進行迭代,但是因爲你的'If'只有'0'和'1''Do somethin''的值。 – Enigmativity

回答

0

你檢查,如果Rows.Count設置,而不是0

如果情況並非如此

For i = 0 To CustomerDataSet.customer.Rows.count - 1 
    if i < 2 Then 
    'Do something 
    Else 
    Continue For 
    End If 
Next 
+0

Veenman行數沒有設置,正如我前面提到的,如果我在其他部分中設置了一個msg框並在每次出現時單擊它,循環將繼續。但我想要的是在其他部分有直接執行到下一行的內容,對於其他部分應該有什麼?請幫忙 – A1990

+0

@ A1990編輯:只要代碼進入Else塊,它將進入For循環的下一個迭代(例如,如果I = 2,它將進入第4次迭代(然後我= 3))我認爲這是你正在尋找的 – DieVeenman

+1

執行將繼續到下一行沒有在else語句中的任何代碼 – Fabio

0

使用Exit For如果你不想條件後返回迭代行false

For i = 0 To CustomerDataSet.customer.Rows.count - 1 
    If condition = false Then Exit For 
    'Do something for rows which condition = true 
Next 

使用Continue For如果你想迭代到下一行不執行你的DoSomething代碼

For i = 0 To CustomerDataSet.customer.Rows.count - 1 
    If condition = false Then Continue For 
    'Do something for rows which condition = true 
Next