Solved

Redim Statement requires an array

Posted on 2010-11-19
5
879 Views
Last Modified: 2013-11-27
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
Comment
Question by:ReneeM787
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Den_HBR
ID: 34176845
Dim strData(3, 0) As String
0
 
LVL 8

Expert Comment

by:Den_HBR
ID: 34176903
Or maybe even a little better... use: Dim strData(,) As String
0
 

Author Comment

by:ReneeM787
ID: 34177659
That worked, Den.  Both options removed the error.  Can you give a brief explanation of why the 2nd option is better?  Thank you!
0
 
LVL 8

Accepted Solution

by:
Den_HBR earned 500 total points
ID: 34179577
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
 

Author Closing Comment

by:ReneeM787
ID: 34180922
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now