Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET File I/O and Registry (http://www.xtremevbtalk.com/-net-file-i-o-and-registry/)
-   -   Length can't be less than zero (http://www.xtremevbtalk.com/-net-file-i-o-and-registry/327615-length-cant-zero.html)

magoteli 05-12-2015 08:39 AM

Length can't be less than zero
 
Hi, i'm trying to use this code to read a .txt file, but i am getting a error in the line:

.medico = Ler_linha_de_registos.Substring(P10, P11 - P10 - 1)

the error:
http://i.imgur.com/dn78smg.png?1

it says:
"Length can't be less than zero - ArgumentOutOfRangeException was unhandled"

I don't know what to do :S

Here's the code:


Code:

  Public Sub Ler_Registo_Requisitados()
        Try
            Dim FSFicheiro_registos As New FileStream(caminho, FileMode.Open, FileAccess.Read)
            Dim SRRegisto_Requisitados As New StreamReader(FSFicheiro_registos)
            Dim Ler_linha_de_registos As String
            Dim P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12 As Integer 'Para guardar a posição final de cada campo da linha de registo
            lst_nomes.Items.Clear() 'Limpar a ListBox
            Do Until SRRegisto_Requisitados.Peek = -1
                Ler_linha_de_registos = SRRegisto_Requisitados.ReadLine
                P1 = Ler_linha_de_registos.IndexOf(";") + 1 'Atibuição da posição do final de cada campo
                P2 = Ler_linha_de_registos.IndexOf(";", P1) + 1
                P3 = Ler_linha_de_registos.IndexOf(";", P2) + 1
                P4 = Ler_linha_de_registos.IndexOf(";", P3) + 1
                P5 = Ler_linha_de_registos.IndexOf(";", P4) + 1
                P6 = Ler_linha_de_registos.IndexOf(";", P5) + 1
                P7 = Ler_linha_de_registos.IndexOf(";", P6) + 1
                P8 = Ler_linha_de_registos.IndexOf(";", P7) + 1
                P9 = Ler_linha_de_registos.IndexOf(";", P8) + 1
                P10 = Ler_linha_de_registos.IndexOf(";", P9) + 1
                P11 = Ler_linha_de_registos.IndexOf(";", P10) + 1
                P12 = Ler_linha_de_registos.IndexOf(";", P11) + 1
                With bs
                    .nome = Ler_linha_de_registos.Substring(0, P1 - 1)
                    .morada = Ler_linha_de_registos.Substring(P1, P2 - P1 - 1)
                    .idade = Ler_linha_de_registos.Substring(P2, P3 - P2 - 1)
                    .datanasc = Ler_linha_de_registos.Substring(P3, P4 - P3 - 1)
                    .freg = Ler_linha_de_registos.Substring(P4, P5 - P4 - 1)
                    .motivo = Ler_linha_de_registos.Substring(P5, P6 - P5 - 1)
                    .sitsocial = Ler_linha_de_registos.Substring(P6, P7 - P6 - 1)
                    .dataatendimento = Ler_linha_de_registos.Substring(P7, P8 - P7 - 1)
                    .nragregado = Ler_linha_de_registos.Substring(P8, P9 - P8 - 1)
                    .rendimento = Ler_linha_de_registos.Substring(P9, P10 - P9 - 1)
                    .medico = Ler_linha_de_registos.Substring(P10, P11 - P10 - 1)
                    .apoios = Ler_linha_de_registos.Substring(P11)
                End With
                lst_nomes.Items.Add(bs.nome) 'Adicionar o nome do utente à ListBox
            Loop
            SRRegisto_Requisitados.Close()
        Catch ex As System.IO.FileNotFoundException 'Tratamento de erro na abertura do ficheiro
            mensagem = "O ficheiro de armazenamento de dados não foi encontrado" & vbNewLine & "Deseja criar um novo ficheiro?"
            titulo = "Ficheiro de Registos"
            botoes = MessageBoxButtons.YesNo
            icone = MessageBoxIcon.Question
            resposta = MessageBox.Show(mensagem, titulo, botoes, icone)
            If resposta = Windows.Forms.DialogResult.Yes Then
                Dim FSFicheiro_registos As New FileStream(caminho, FileMode.CreateNew)
                FSFicheiro_registos.Close()
            Else
                Me.Close()
            End If
        End Try
    End Sub


Gruff 05-12-2015 10:15 AM

The arguments for the substring() method as you are using it are:

Substring(<Start Position>,<Length of Substring>)

Your calculated Length parameter is a negative number at some point.

Put a break point at the offending offending line, run the program and use debug to see if this is true.

Most probably your logic does not work for all cases of data from the file.
adjust your code or program design to not allow that to happen.

loquin 05-13-2015 04:49 PM

1 Attachment(s)
Gruff is correct.

But, rather than trying to reinvent the wheel, since it appears that you are simply trying to split the 'fields' from the source string, use the SPLIT Function. i.e. Something along this approach:
Code:
Dim s() = SPLIT(Ler_linha_de_registos,";") ' OR Dim s() = Ler_linha_de_registos.Split(";") At this point, your array s has 12 elements. Now, assign to the fields With bs .nome = s(0) .morada = s(1) .idade = s(2) ' ... .apoios =s(11) End With

One final point - when you link to an image, rather than attach the image directly to the message using the Manage Attachments button, the availability of that image here is dependent upon the another site. If the other site is down for any reason, or you remove the image from that site, then the content goes away here...:mad:


All times are GMT -6. The time now is 09:18 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.