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
Index Out of Bounds?
Page 1 of 111 Replies - 349 Views - Last Post: 16 February 2012 - 06:17 AM
Topic Sponsor:
#1
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.
Replies To: Index Out of Bounds?
#2
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'
#3
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:
You always write the whole directory adrress everywhere
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
#4
Re: Index Out of Bounds?
Posted 07 February 2012 - 10:50 AM
smohd, 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:
You always write the whole directory adrress everywhere
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
#5
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.
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.
#6
Re: Index Out of Bounds?
Posted 09 February 2012 - 05:26 AM
smohd, 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.
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?
#7
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)?
This way you will only attempt to use the 13th element if it exists.
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.
#8
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.
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.
#10
Re: Index Out of Bounds?
Posted 09 February 2012 - 06:34 AM
CharlieMay, 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)?
This way you will only attempt to use the 13th element if it exists.
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.
#11
Re: Index Out of Bounds?
Posted 09 February 2012 - 07:29 AM
Always post your updated code so we can go together
#12
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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|