Solved

redim array problem

Posted on 2000-04-10
7
295 Views
Last Modified: 2011-09-20
Having problem with reading a sequential access file into an array and calculating average and high score.

i think its the way i declare the redim?

Any suggestions??

Slammer14
Option Explicit

'Dim intScoreArray(1 To 20) As Integer
Dim intNumData As Integer
Dim intRecCount As Integer

Private Sub cmdAvg_Click()
    Dim intNum As Integer, intTotal As Integer, sngAvg As Single
    For intNum = 1 To intRecCount
        intTotal = intTotal + intScoreArray(intNum)   'accumulate array scores
    Next intNum
    sngAvg = intTotal / intRecCount                        'calculate average
    lblMsg.Caption = "Average"                        'display results
    lblAnswer.Caption = Format(sngAvg, "standard")
End Sub

Private Sub cmdExit_Click()
    Unload frmTest
End Sub

Private Sub cmdHigh_Click()
    Dim intNum As Integer, intHigh As Integer
    intHigh = intScoreArray(1)         'initialize intHigh to first array element
    For intNum = 2 To intNumData               'compare intHigh to remaining array elements
        If intScoreArray(intNum) > intHigh Then  'element is greater than intHigh
            intHigh = intScoreArray(intNum)
        End If
    Next intNum
    lblMsg.Caption = "Highest"         'display results
    lblAnswer.Caption = intHigh
End Sub

Private Sub cmdUpdate_Click()
    Dim intNum As Integer, intIncrease As Integer
    intIncrease = Val(InputBox("Enter the number of additional points:"))
    For intNum = 1 To intNumData        'add additional points to each score in the array
        intScoreArray(intNum) = intScoreArray(intNum) + intIncrease
    Next intNum
    lblMsg.Caption = "Scores updated by"        'display message
    lblAnswer.Caption = intIncrease
   
    Open "a:\Tut10\Test11.dat" For Output As #1   'open file
    For intNum = 1 To intNumData                      'write array contents to file
        Write #1, intScoreArray(intNum)
    Next intNum
    Close #1                                    'close file
End Sub
Private Sub Form_Load()

    frmTest.Left = (Screen.Width - frmTest.Width) / 2
    frmTest.Top = (Screen.Height - frmTest.Height) / 2
   
    Dim intNum As Integer, strNext As String, intRecord As Integer
    Dim i As Integer
    Open "a:\Tut10\test11.dat" For Input As #1            'open file
   
    intRecord = 0
   
    Do While Not EOF(1)
        ReDim intScoreArray(0) As Integer
        Input #1, intNum
        ReDim Preserve intScoreArray(1 To intNum) As Integer
             For i = 1 To intNumData
       
            Input #1, intScoreArray(i)
            intRecCount = intRecCount + 1                    ' establish amount of records for upperbound
            Next
    Loop
  '
    Close #1
   
End Sub




                       


0
Comment
Question by:slammer14
7 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2702666
I am not sure but i don't think you can REDIM Array that has been dimentioned.
i tried it like this and it gave error.
if array is dimintioned ex.
Dim r(4) as integer and then you redim you get an error array dimentioned
but if array was declared without given dimention like
Dim r() as integer and then you redim it
Redim r(1 to 10) as integer
no error will occur.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2702725
Do While Not EOF(1)
        ReDim intScoreArray(0) As Integer
        Input #1, intNum
        ReDim Preserve intScoreArray(1 To intNum) As Integer
             For i = 1 To intNumData
         
            Input #1, intScoreArray(i)
            intRecCount = intRecCount + 1                    ' establish amount of records for upperbound
            Next
    Loop
 

I dont see why you are using two Redim statements here.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2702789
also you state For i = 1 to intNumData in your form load event.  I see where you declare intNumData but I dont see anywhere where you assign a value to it.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 14

Expert Comment

by:wsh2
ID: 2702836
Too much to comment on.. take a look at this.. <huge smile>.. Great to SIEZE ya again !!!

<----- Code Begin ----->

Option Explicit

Private m_intCount As Integer
Private m_intScores(1 To 20) As Integer
Private m_strFilePath As String

Private Sub cmdAvg_Click()
   
   Dim intTotal As Integer
   Dim intIndex As Integer
   For intIndex = 1 To m_intCount
       intTotal = intTotal _
           + m_intScores(intIndex) ' accumulate array scores
   Next intIndex
   
   lblMsg.Caption = "Average" ' display results
   Dim sngAverage As Single
   sngAverage = intTotal / m_intCount ' calculate average
   lblAnswer.Caption = Format(sngAverage, "standard")

End Sub

Private Sub cmdExit_Click()
   
    Unload frmTest

End Sub

Private Sub cmdHigh_Click()
   
   Dim intMaximum As Integer
   intMaximum = m_intScores(1) ' initialize intMaximum to first array element
   
   Dim intIndex As Integer
   For intIndex = 2 To m_intCount ' compare intMaximum to remaining array elements
      If m_intScores(intIndex) > intMaximum _
      Then  'element is greater than intMaximum
         intMaximum = m_intScores(intIndex)
      End If
   Next intIndex
   
   lblMsg.Caption = "Highest" ' display results
   lblAnswer.Caption = intMaximum

End Sub
Private Sub cmdUpdate_Click()
   
   Dim intTotal As Integer
   Dim intIndex As Integer
   For intIndex = 1 To m_intCount ' add additional points to each score in the array
      m_intScores(intIndex) = Val(InputBox( _
         "Enter the number of points:", _
         "Entry# " & intIndex & " Of " & m_intCount, _
         m_intScores(intIndex) _
         ))
      intTotal = intTotal + m_intScores(intIndex)
   Next intIndex
   
   lblMsg.Caption = "Scores Total " ' display message
   lblAnswer.Caption = intTotal
   
   On Error Resume Next
   Kill m_strFilePath
   On Error GoTo 0
   Open m_strFilePath For Output As #1 ' open file
   For intIndex = 1 To m_intCount ' write array contents to file
       Write #1, m_intScores(intIndex)
   Next intIndex
   Close #1 ' close file

End Sub

Private Sub Form_Load()

   frmTest.Left = (Screen.Width - frmTest.Width) / 2
   frmTest.Top = (Screen.Height - frmTest.Height) / 2
   
   m_strFilePath = "a:\Tut10\test11.dat"

   Dim intTotal As Integer
   On Error Resume Next
   Open m_strFilePath For Input As #1 ' open file
   If Err.Number > 0 _
   Then
      Err.Clear
   Else
      Dim intIndex As Integer
      Do Until EOF(1) _
      Or intIndex = UBound(m_intScores)
        intIndex = intIndex + 1
        Input #1, m_intScores(intIndex)
      Loop
   End If
   Close #1
   
   lblMsg.Caption = "Scores Loaded " ' display message
   lblAnswer.Caption = intIndex
   m_intCount = UBound(m_intScores)
     
End Sub

<----- Code End ----->
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 2703488
Try this


Option Base 1


Dim intScoreArray() As Integer

to redim the array:

ReDim Preserve intScoreArray(intNum)
             
To erase the array

erase intScoreArray


0
 

Expert Comment

by:sreekrish
ID: 2703528
Redim will give you error in the following case

Dim iaScore(4) as integer
....
....
....
....
Redim iaScore(20) as integer

-- error because iaScore should not be dimensioned when declare the dimension statement should be dim a() as integeriin that case it will work.
0
 
LVL 11

Accepted Solution

by:
bmatumbura earned 50 total points
ID: 2712777
If you cut and pasted your code above then the line after Option Explicit should read:

  Dim intScoreArray() As Integer

to declare a dynamic array.

You can use the Dim statement with empty parentheses to declare a dynamic array. After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public, or Dim statement, an error occurs.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

21 Experts available now in Live!

Get 1:1 Help Now