Solved

redim array problem

Posted on 2000-04-10
7
297 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
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.

 
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

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

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 56
Windows 10 start screen issues 9 51
VB6 Compile Compatibility Issue 4 90
TT Auto Dashboard 13 85
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 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

20 Experts available now in Live!

Get 1:1 Help Now