Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only first 1001 entries of integer array writable? VS2008 2

Status
Not open for further replies.

himynameisbunny

Technical User
May 19, 2009
3
AU
I have officially spent more than two hours on this single problem...

I have got it to work for the most part...however I have a little issue - being that when Button 1 is clicked, it will only write to the first 1001 slots of the array, and the RichTextBox I have it writing to will only show those 1001 entries.

However, when sorted, it will show all 2000 entries contained in the array, INCLUDING all the 0 values that weren't written to in the previous step. It will write to the adjacent Rich Text Box as well as to file like this.

A picture of my GUI is attached.

Help would be much appreciated, cheers.

Code:
Imports System.IO

Public Class Form4

    Dim ArrNums(2000) As Integer
    Dim r As Random = New Random(0)
    Dim i As Integer

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Me.i = 0 To ArrNums.Length
            ArrNums(i) = r.Next(1, 10000)
            RichTextBox1.Text = RichTextBox1.Text & ArrNums(i) & vbCrLf
            i = i + 1
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Array.Sort(ArrNums)

        For Me.i = 0 To ArrNums.Length
            RichTextBox2.Text = RichTextBox2.Text & ArrNums(i) & vbCrLf
            i = i + 1
        Next

        Dim SaveFileDialog As New SaveFileDialog
        SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        SaveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

        If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            Dim FileName As String = SaveFileDialog.FileName
            Dim objSW As StreamWriter
            Dim objFS As FileStream

            objFS = New FileStream(FileName, FileMode.Create)
            objSW = New StreamWriter(objFS)

            For Me.i = 0 To ArrNums.Length
                objSW.WriteLine(ArrNums(i))
                i = i + 1
            Next

            objSW.Close()
            objFS.Close()
        End If
    End Sub
End Class
 
Throws an "IndexOutOfRangeException was unhandled" error if I remove that (should really have some sort of error handling in there...) due to the machine not knowing when to stop reading the array...

I'm out of ideas :-(
 
That should be because of
Code:
 For Me.i = 0 To ArrNums.Length
change it to
Code:
 For Me.i = 0 To ArrNums.Length-1

Also in .net language
Code:
RichTextBox1.Text = RichTextBox1.Text & ArrNums(i) & vbCrLf
can be converted to
Code:
RichTextBox1.AppendText(ArrNums(i) & vbCrLf)
Code:
  i = i + 1
to
Code:
i += 1

Zameer Abdulla
 

Try this:
Code:
Public Class Form1
    Dim ArrNums(2000) As Integer
    Dim r As Random = New Random(0)
    Dim i As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
        For Me.i = 0 To ArrNums.Length - 1
            ArrNums(i) = r.Next(1, 10000)
            RichTextBox1.AppendText(ArrNums(i) & vbCrLf)
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button2.Click
        Array.Sort(ArrNums)
        For Me.i = 0 To ArrNums.Length - 1
            RichTextBox2.AppendText(ArrNums(i) & vbCrLf)
        Next
    End Sub
End Class
No need for i = i + 1

Have fun.

---- Andy
 
Code:
Try this:
[snip]
No need for i = i + 1

Thanks, that did it!

Must have been the combination of having the i += 1 and not having -1 after ArrNums.Length that did it ?_?

Thanks for the help guys, really appreciate it :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top