Length can't be less than zero
Length can't be less than zero
Length can't be less than zero
Length can't be less than zero
Length can't be less than zero
Length can't be less than zero Length can't be less than zero Length can't be less than zero Length can't be less than zero Length can't be less than zero Length can't be less than zero Length can't be less than zero Length can't be less than zero
Length can't be less than zero Length can't be less than zero
Length can't be less than zero
Go Back  Xtreme Visual Basic Talk > > > Length can't be less than zero


Reply
 
Thread Tools Display Modes
  #1  
Old 05-12-2015, 08:39 AM
magoteli magoteli is offline
Newcomer
 
Join Date: May 2015
Posts: 1
Default 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
Reply With Quote
  #2  
Old 05-12-2015, 10:15 AM
Gruff's Avatar
GruffLength can't be less than zero Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

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.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 05-13-2015, 04:49 PM
loquin's Avatar
loquinLength can't be less than zero loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,400
Default

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...
Attached Images
File Type: png dn78smg.png (66.9 KB, 4 views)
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown

Last edited by loquin; 05-13-2015 at 06:09 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Length can't be less than zero
Length can't be less than zero
Length can't be less than zero Length can't be less than zero
Length can't be less than zero
Length can't be less than zero
Length can't be less than zero Length can't be less than zero Length can't be less than zero Length can't be less than zero Length can't be less than zero Length can't be less than zero Length can't be less than zero
Length can't be less than zero
Length can't be less than zero
 
Length can't be less than zero
Length can't be less than zero
 
-->