Error check outside repeat loop

Asked

Viewed 28 times

0

I need to verify that the contents of a file are in alphabetical order, but I want you to display only one error check message. With the code I am using, it displays the 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?

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

This way the error will be displayed at each incorrect line.

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

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

  • And to stop the loop at the first occurrence?

  • I edited the answer!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.