Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Redim Statement requires an array

Posted on 2010-11-19
5
Medium Priority
?
958 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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