Solved

Redim Statement requires an array

Posted on 2010-11-19
5
862 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
Comment Utility
Dim strData(3, 0) As String
0
 
LVL 8

Expert Comment

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

Author Comment

by:ReneeM787
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

18 Experts available now in Live!

Get 1:1 Help Now