• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1002
  • Last Modified:

Redim Statement requires an array

I am working on a class assignment.  Some of the code is provided, and I am trying to work out the 'holes' to fill in.  The declarations are mine, so it is entirely possible something there is off there.

The point of the assignment is to view a Title, Date and Memo stored in a text file.  I am getting the error Redim Statement requires an array on line 33.  Can you help me understand what I need to change?  Thank you.
Public Class frmMemoryHelper
    Dim strCurrentFile As String
    Dim StreamReader As String
    Dim intMax As Integer
    Dim intPos As Integer
    Dim strDummy1 As String
    Dim strDummy2 As String
    Dim strDummy3 As String
    Dim strData As String

    Private Sub mnuMain_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles mnuMain.ItemClicked

        Dim intX As Integer
        Me.dlgOpen.Filter = "Text files (*.txt) | *.txt|All files (*.*)|*.*"
        Me.dlgOpen.InitialDirectory = Application.StartupPath
        Me.dlgOpen.FilterIndex = 1
        Me.dlgOpen.Title = "Open a Memory Helper File"

        If Me.dlgOpen.ShowDialog() = DialogResult.OK Then 'display dialog and accept user input
            strCurrentFile = Me.dlgOpen.FileName
            Dim objReader As IO.StreamReader = New IO.StreamReader(strCurrentFile)
            intMax = 0
            strDummy3 = "Dummy Value"
            Do Until strDummy3 = Nothing
                strDummy1 = objReader.ReadLine
                strDummy2 = objReader.ReadLine
                strDummy3 = objReader.ReadLine
                intMax = intMax + 1
            Loop
            intMax = intMax - 1
            objReader.Close()
            objReader = Nothing
            ReDim strData(3, intMax)    'Make the array the size of the # of records
            Dim objReader1 As IO.StreamReader = New IO.StreamReader(strCurrentFile)
            For intX = 1 To intMax      'Read all records to the array
                strData(1, intX) = objReader1.ReadLine
                strData(2, intX) = objReader1.ReadLine
                strData(3, intX) = objReader1.ReadLine
            Next
            objReader1.Close()
            objReader1 = Nothing
            intPos = 1
            Fill_Screen()               'Put data in the text boxes.
        End If

    End Sub

    Sub Fill_Screen()
        Me.txtTitle.Text = strData(1, intPos)
        Me.txtDate.Text = strData(2, intPos)
        Me.txtMemo.Text = strData(3, intPos)
    End Sub

End Class

Open in new window

0
ReneeM787
Asked:
ReneeM787
  • 3
  • 2
1 Solution
 
Den_HBRCommented:
Dim strData(3, 0) As String
0
 
Den_HBRCommented:
Or maybe even a little better... use: Dim strData(,) As String
0
 
ReneeM787Author Commented:
That worked, Den.  Both options removed the error.  Can you give a brief explanation of why the 2nd option is better?  Thank you!
0
 
Den_HBRCommented:
You will NEVER notice any difference between both options
But...

With the second option you are not yet reservating memory.
If you use the first option and for some reason your Sub mnuMain_ItemClicked is never called, your program is using more memory than it needs.

Secondly, if you reserve the memory in 2 steps, your allocated memory will probably not be sequential.





0
 
ReneeM787Author Commented:
I am going to post a follow-up question because I am now getting an index out of bounds error when I try to open the .txt file the program should display.  But, since this answer solved the error I posted about, I will repost.  Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now