Checking error out of repeat loop

0

I need to check if the contents of a file are in alphabetical order, but I want it to display only one message with the error check. With the code I'm using, it displays errors according to the amount of items.

Dim local_arquivo As String
Dim c As Char
Dim split As String()
Dim ordemalf As String

DataGridTodos.Rows.Clear()
local_arquivo = TextBox.Text
Dim leitura As New System.IO.StreamReader(local_arquivo, System.Text.Encoding.Default)
c = ";"
While leitura.Peek() <> -1
    split = leitura.ReadLine().Split(c)
    ordemalf = split(1)
    Dim i As Integer
    For i = 0 To split(1).Length - 1
        If ordemalf(0) > "A" Then
            MessageBox.Show("Arquivo incorreto!", "VESCPF", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            DataGridTodos.Rows.Add(split)
        End If
    Next
End While

What am I doing wrong?

    
asked by anonymous 06.05.2016 / 17:26

1 answer

2

If you want to display an error message for each line that does not have the required order you can use boolean.

Dim local_arquivo As String
Dim c As Char
Dim split As String()
Dim ordemalf As String

DataGridTodos.Rows.Clear()
local_arquivo = TextBox.Text
Dim leitura As New System.IO.StreamReader(local_arquivo, System.Text.Encoding.Default)
c = ";"
While leitura.Peek() <> -1
    Dim error As Boolean
    split = leitura.ReadLine().Split(c)
    ordemalf = split(1)
    error = False
    Dim i As Integer
    For i = 0 To split(1).Length - 1
        If ordemalf(0) > "A" Then
            error = True
            Exit For
        Else
            DataGridTodos.Rows.Add(split)
        End If
    Next

    If error = True
        MessageBox.Show("Arquivo incorreto!", "VESCPF", MessageBoxButtons.OK, MessageBoxIcon.Error)
        ' Para o loop na primeira ocorrência
        Exit While
End While

In this way, the error will be displayed with each incorrect line.

The code Exit For is used to exit the Loop when it encounters the error.

The code Exit While is used to stop the Loop on the first occurrence.

    
06.05.2016 / 17:44