?
Solved

Dynamic Array in VB6

Posted on 2005-03-15
4
Medium Priority
?
255 Views
Last Modified: 2012-05-05

Hi,

I have an error stating "9" Subscript out of range


I have a class called CCallCode and 1 function for testing array. I want to use dynamic array for more flexibility

CCallCode
-----------------------------------------------------------------------------------------------------------------------
Option Explicit

Private mCallCodeID As Integer
Private mCallCode As String

Public Property Get CallCodeID() As Integer
    CallCodeID = mCallCodeID
End Property

Public Property Let CallCodeID(ByVal ID As Integer)
    mCallCodeID = ID
End Property

Public Property Get CallCode() As String
    CallCode = mCallCode
End Property

Public Property Let CallCode(ByVal Code As String)
    mCallCode = Code
End Property

-----------------------------------------------------------------------------------------------------------------------


And a function in a form for testing the array:
-----------------------------------------------------------------------------------------------------------------------
Private Sub cmdCallCode2_Click()
   
    Dim CallCode() As CCallCodes
    Dim Index As Integer
   
   
    With CallCode(0)
        .CallCodeID = 1001
        .CallCode = "Nurse Needed"
    End With
   
    With CallCode(1)
        .CallCodeID = 1001
        .CallCode = "Nurse Needed"
    End With
   
    For Index = LBound(CallCode) To UBound(CallCode)
        Print CallCode(Index).CallCodeID
        Print CallCode(Index).CallCode
        Print
    Next Index
   
End Sub
-----------------------------------------------------------------------------------------------------------------------



Thanks
0
Comment
Question by:TungVan
4 Comments
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 140 total points
ID: 13547667
if you are trying to add content to CallCode(0) and CallCode(1), then you must at least inform the Compiler that there will be two elements in the array CallCode() which you have declared as :

Dim CallCode() as CCallCodes

This simply tells the compiler that CallCode will be an array, but no elements are reserved, so you need AT LEAST:

Private Sub cmdCallCode2_Click()
   
    Dim CallCode() As CCallCodes
    Dim Index As Integer
   
Redim Preserve CallCode(2) as CCallCodes
   
    With CallCode(0)
        .CallCodeID = 1001
        .CallCode = "Nurse Needed"
    End With
   
    With CallCode(1)
        .CallCodeID = 1001
        .CallCode = "Nurse Needed"
    End With
   
    For Index = LBound(CallCode) To UBound(CallCode)
        Print CallCode(Index).CallCodeID
        Print CallCode(Index).CallCode
        Print
    Next Index
   
End Sub


However, since you are 1) declaring this 'dynamic' array ENTIRELY in the code of this Sub procedure, and 2) you appear to know that there are 2 elements in the array, why are you making this a dynamic array ???

Code would be better as:

Private Sub cmdCallCode2_Click()
   
    Dim CallCode(2) As CCallCodes
    Dim Index As Integer
   
   
    With CallCode(0)
        .CallCodeID = 1001
        .CallCode = "Nurse Needed"
    End With
   
    With CallCode(1)
        .CallCodeID = 1001
        .CallCode = "Nurse Needed"
    End With
   
    For Index = LBound(CallCode) To UBound(CallCode)
        Print CallCode(Index).CallCodeID
        Print CallCode(Index).CallCode
        Print
    Next Index
   
End Sub

What are you trying to accomplish?  Why do you think you need to use a dynmic array?

AW
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 140 total points
ID: 13547679
You need to dimension your array, either initially:

Dim CallCode(0 to 10) As CCallCodes


or after declaration:

Dim CallCode() As CCallCodes

Redim CallCode(0 to 10) As CCallCodes


Once it has been dimensioned, you can redimension using:

Redim Preserve CallCode(0 to 20) As CCallCodes

The 'Preserve' keyword tells VB not to erase the contents currently in the array.
0
 
LVL 26

Assisted Solution

by:EDDYKT
EDDYKT earned 120 total points
ID: 13547697
Again i'm not sure why you don't use collection?

It is build in collection that you don't have to take care of index and searching
0
 

Author Comment

by:TungVan
ID: 13547810


Hi

EDDYKT proposed collection object...And i tried it, it is very easy and I think i will go with collection.....thanks...I didn't know collection object before you mentionned about it (m a newbie in VB)

To explain why I have questions on dynamic array, link list and collection: I want to have the most flexible record structure, so that later on, i can add & remove object class easily. I think collection provides me this...

But all of your replies help me a lot to understand how dynamic arrays & collection works, so i decide to split the pts..:)
 
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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.
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…
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…
Suggested Courses

621 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