对于具有If条件的循环 - 如果条件未满足则进行迭代

时间:2015-10-29 10:46:39

标签: vb.net if-statement for-loop

我有这个代码,我希望这个for循环迭代每一行。但是当if条件为假时,此代码不会迭代 我在其他部分试过“继续”。但它没有用。即使我将msgbox放入else部分并在每次显示时单击msg框,然后循环继续。但它不实用,因为我要检查1000行。

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帮助我在没有任何用户交互的情况下继续循环。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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