11 Replies - 349 Views - Last Post: 16 February 2012 - 06:17 AM Rate Topic: -----

Topic Sponsor:

#1 knutter539  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 31-May 11

Index Out of Bounds?

Posted 07 February 2012 - 08:45 AM

So this code is a section taken from a larger app and its purpose is to read the lines in a text file and place them in certain comboboxes, textboxes, etc. it works great right now except i have hit a snag. line 13 is the name of the project leader who was in charge of the job. However on older files we didnt include that name so line 13 isnt there. So when i debug the app will load up with nothing in that combobox(2) which is expected, but when i hit the update and apply button it errors out saying "Index was outside the bounds of the array" regardless of whats in the box. thank you in advance for any advice, been stuck on this for a month now.

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If System.IO.File.Exists("c:\temp\test2.txt") Then

            Dim theFile As String = "c:\temp\test2.txt"
            Dim lines() As String = System.IO.File.ReadAllLines("c:\temp\test2.txt")


            'IF Line 13 (leader name) isnt in the text file, the application crashes at "lines(12) = Combobox2.text"
            ' we need to have the app count the lines first and then run the below code even if the last line isnt in the
            ' text file.

            lines(0) = RichTextBox1.Text
            lines(1) = TextBox6.Text
            lines(2) = ComboBox1.Text
            lines(3) = TextBox7.Text
            lines(4) = TextBox5.Text
            lines(5) = TextBox4.Text
            lines(6) = ComboBox5.Text
            lines(7) = ComboBox6.Text
            lines(8) = ComboBox3.Text
            lines(9) = ComboBox4.Text
            lines(10) = ComboBox7.Text
            lines(11) = ComboBox9.Text
            lines(12) = ComboBox2.Text
            System.IO.File.WriteAllLines("c:\temp\test2.txt", lines)
        Else

            Dim Writer As New StreamWriter("C:\temp\test2.txt", False)
            Writer.Write(RichTextBox1.Text & Environment.NewLine)
            Writer.Write(TextBox6.Text & Environment.NewLine)
            Writer.Write(ComboBox1.Text & Environment.NewLine)
            Writer.Write(TextBox7.Text & Environment.NewLine)
            Writer.Write(TextBox5.Text & Environment.NewLine)
            Writer.Write(TextBox4.Text & Environment.NewLine)
            Writer.Write(ComboBox5.Text & Environment.NewLine)
            Writer.Write(ComboBox6.Text & Environment.NewLine)
            Writer.Write(ComboBox3.Text & Environment.NewLine)
            Writer.Write(ComboBox4.Text & Environment.NewLine)
            Writer.Write(ComboBox7.Text & Environment.NewLine)
            Writer.Write(ComboBox9.Text & Environment.NewLine)
            Writer.Write(ComboBox2.Text & Environment.NewLine)
            Writer.Close()

        End If



    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim result As String = TextBox1.Text
        If result.Length >= 5 Then
            result = result.Substring(result.Length - 5)
            TextBox2.Text = result
        End If
        If Not System.IO.File.Exists("c:\temp\test2.txt") Then
            'System.IO.File.CreateText("c:\temp\test2.txt")

        Else
            Dim Tr1 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim job As String
            job = Split(Tr1.ReadToEnd(), vbCrLf)(0)
            RichTextBox1.Text = job
            Tr1.Close()

            Dim Tr2 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim Cust As String
            Cust = Split(Tr2.ReadToEnd(), vbCrLf)(1)
            TextBox6.Text = Cust
            Tr2.Close()

            Dim Tr3 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim Leader As String
            Leader = Split(Tr3.ReadToEnd(), vbCrLf)(2)
            ComboBox1.Text = Leader
            Tr3.Close()

            Dim Tr4 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim DD As String
            DD = Split(Tr4.ReadToEnd(), vbCrLf)(3)
            TextBox7.Text = DD
            Tr4.Close()

            Dim Tr5 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim WO As String
            WO = Split(Tr5.ReadToEnd(), vbCrLf)(4)
            TextBox5.Text = WO
            Tr5.Close()

            Dim Tr6 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim log As String
            log = Split(Tr6.ReadToEnd(), vbCrLf)(5)
            TextBox4.Text = log
            Tr6.Close()

            Dim Tr7 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim mat As String
            mat = Split(Tr7.ReadToEnd(), vbCrLf)(6)
            ComboBox5.Text = mat
            Tr7.Close()

            Dim Tr8 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim ftype As String
            ftype = Split(Tr8.ReadToEnd(), vbCrLf)(7)
            ComboBox6.Text = ftype
            Tr8.Close()

            Dim Tr9 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim rh As String
            rh = Split(Tr9.ReadToEnd(), vbCrLf)(8)
            ComboBox3.Text = rh
            Tr9.Close()

            Dim Tr10 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim lh As String
            lh = Split(Tr10.ReadToEnd(), vbCrLf)(9)
            ComboBox4.Text = lh
            Tr10.Close()

            Dim Tr11 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim rev As String
            rev = Split(Tr11.ReadToEnd(), vbCrLf)(10)
            ComboBox7.Text = rev
            Tr11.Close()

            Dim Tr12 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim setnum As String
            setnum = Split(Tr12.ReadToEnd(), vbCrLf)(11)
            ComboBox9.Text = setnum
            Tr12.Close()

            Dim Tr13 As IO.TextReader = System.IO.File.OpenText("c:\temp\test2.txt")
            Dim cad As String
            cad = Split(Tr13.ReadToEnd(), vbCrLf)(12)
            ComboBox2.Text = cad
            Tr13.Close()

        End If
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Index Out of Bounds?

#2 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 67
  • View blog
  • Posts: 522
  • Joined: 21-December 11

Re: Index Out of Bounds?

Posted 07 February 2012 - 09:52 AM

You can get number of lines with 'upper bound' function:
Dim intNumberOfLines as Integer = UBound(Lines) + 1' 



Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Index Out of Bounds?

Posted 07 February 2012 - 10:22 AM

The the simple way is to check how many lines you have before adding them, and add only lines which are available...

But wait, if you need to write to file what you have, then why you read it first? Just create your array with the size 12 as you need and give the array values and write it.

Also I dont see the use of this variable here:
Dim theFile As String = "c:\temp\test2.txt"

You always write the whole directory adrress everywhere
Was This Post Helpful? 1
  • +
  • -

#4 knutter539  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 31-May 11

Re: Index Out of Bounds?

Posted 07 February 2012 - 10:50 AM

View Postsmohd, on 07 February 2012 - 10:22 AM, said:

The the simple way is to check how many lines you have before adding them, and add only lines which are available...

But wait, if you need to write to file what you have, then why you read it first? Just create your array with the size 12 as you need and give the array values and write it.

Also I dont see the use of this variable here:
Dim theFile As String = "c:\temp\test2.txt"

You always write the whole directory adrress everywhere


i need it to read the file if it exists already, then allow me to change information in the form if needed and overwrite it thats why it is reading it first
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Index Out of Bounds?

Posted 07 February 2012 - 11:18 AM

Just use StreamWriter with append flag to false which will overwrite the available data, then this will give no need for that unnecessary reading.
Then just create an array of the size which you need and add content to it, or just do like what you did in else part, becauce I dont see the different.
In both two cases you are just adding new data to a file.
Was This Post Helpful? 1
  • +
  • -

#6 knutter539  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 31-May 11

Re: Index Out of Bounds?

Posted 09 February 2012 - 05:26 AM

View Postsmohd, on 07 February 2012 - 11:18 AM, said:

Just use StreamWriter with append flag to false which will overwrite the available data, then this will give no need for that unnecessary reading.
Then just create an array of the size which you need and add content to it, or just do like what you did in else part, becauce I dont see the different.
In both two cases you are just adding new data to a file.



don't i already have append set to false for my Streamwriter?
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Index Out of Bounds?

Posted 09 February 2012 - 05:58 AM

I guess the way I see this is in your code as you have it, your issue is that you either have lines with 12 elements or 13 elements depending on the file so could you just check against the lines.count prior to trying to assign to an out-of-bound element number (12 in this case)?

If lines.count = 13 then
  lines(12) = combobox2.text
End If

This way you will only attempt to use the 13th element if it exists.
Was This Post Helpful? 1
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Index Out of Bounds?

Posted 09 February 2012 - 06:19 AM

I gave that Idea because in both cases you are overwriting those files, regardless if they were there or not. Look at your first condition and you will see you are doing exact the same thing as overwriting it.
You read all lines and then give all lines other values!! and store those new lines. Now the problem comes for old files which has no line 13, when reading the array created will be of size 11.
Also you can check the size of the array created and Redem it to add one more value if it is less.
Was This Post Helpful? 1
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Index Out of Bounds?

Posted 09 February 2012 - 06:31 AM

Sorry smohd, I completely missed that :oops:
Was This Post Helpful? 0
  • +
  • -

#10 knutter539  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 31-May 11

Re: Index Out of Bounds?

Posted 09 February 2012 - 06:34 AM

View PostCharlieMay, on 09 February 2012 - 05:58 AM, said:

I guess the way I see this is in your code as you have it, your issue is that you either have lines with 12 elements or 13 elements depending on the file so could you just check against the lines.count prior to trying to assign to an out-of-bound element number (12 in this case)?

If lines.count = 13 then
  lines(12) = combobox2.text
End If

This way you will only attempt to use the 13th element if it exists.


hmm so i threw that code in above my array and now im getting a bug after the array saying that it cant write the new data i enter because that text file is being used already.
Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Index Out of Bounds?

Posted 09 February 2012 - 07:29 AM

Always post your updated code so we can go together
Was This Post Helpful? 1
  • +
  • -

#12 knutter539  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 31-May 11

Re: Index Out of Bounds?

Posted 16 February 2012 - 06:17 AM

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If System.IO.File.Exists("c:\temp\test2.txt") Then

            Dim theFile As String = "c:\temp\test2.txt"
            Dim lines() As String = System.IO.File.ReadAllLines("c:\temp\test2.txt")



            ' If totallines = 13 Then
            '    Run array below
            ' Else
            '       Run array without the last line included
            ' End If

            If lines.Count = 13 Then

            lines(0) = RichTextBox1.Text
            lines(1) = TextBox6.Text
            lines(2) = ComboBox1.Text
            lines(3) = TextBox7.Text
            lines(4) = TextBox5.Text
            lines(5) = TextBox4.Text
            lines(6) = ComboBox5.Text
            lines(7) = ComboBox6.Text
            lines(8) = ComboBox3.Text
            lines(9) = ComboBox4.Text
            lines(10) = ComboBox7.Text
            lines(11) = ComboBox9.Text
            lines(12) = ComboBox2.Text
                System.IO.File.WriteAllLines("c:\temp\test2.txt", lines)
            End If

            If lines.Count = 12 Then
                lines(0) = RichTextBox1.Text
                lines(1) = TextBox6.Text
                lines(2) = ComboBox1.Text
                lines(3) = TextBox7.Text
                lines(4) = TextBox5.Text
                lines(5) = TextBox4.Text
                lines(6) = ComboBox5.Text
                lines(7) = ComboBox6.Text
                lines(8) = ComboBox3.Text
                lines(9) = ComboBox4.Text
                lines(10) = ComboBox7.Text
                lines(11) = ComboBox9.Text
                System.IO.File.WriteAllLines("c:\temp\test2.txt", lines)
            End If
        Else

            Dim Writer As New StreamWriter("C:\temp\test2.txt", False)
            Writer.Write(RichTextBox1.Text & Environment.NewLine)
            Writer.Write(TextBox6.Text & Environment.NewLine)
            Writer.Write(ComboBox1.Text & Environment.NewLine)
            Writer.Write(TextBox7.Text & Environment.NewLine)
            Writer.Write(TextBox5.Text & Environment.NewLine)
            Writer.Write(TextBox4.Text & Environment.NewLine)
            Writer.Write(ComboBox5.Text & Environment.NewLine)
            Writer.Write(ComboBox6.Text & Environment.NewLine)
            Writer.Write(ComboBox3.Text & Environment.NewLine)
            Writer.Write(ComboBox4.Text & Environment.NewLine)
            Writer.Write(ComboBox7.Text & Environment.NewLine)
            Writer.Write(ComboBox9.Text & Environment.NewLine)
            Writer.Write(ComboBox2.Text & Environment.NewLine)
            Writer.Close()

        End If



    End Sub



yea smohd you are correct. most of the time i have files with 13 elements but older text files didint have the last line included so it needs to be able to not crash when we update it.

So i wrote two separate IF statements and it seems the issue now is if there are 12 lines i get a bug saying the file is already open and cant get past the System.IO.File.WriteAllLines("c:\temp\test2.txt", lines) line.

This post has been edited by knutter539: 16 February 2012 - 06:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1