?
Solved

Dynamic Array in VB6

Posted on 2005-03-15
4
Medium Priority
?
248 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
[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
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

Industry Leaders: 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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

764 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