I am new to Visual Basic 6 Class Modules and am having difficulty constructing a certain class.

First off, I created a simple class, which is basically an array with some basic properties (Sum, etc).

Next off, I want to create another class, which has these arrays as properties. As expected, I can do this by declaring the first class as public variables in the class module. However, this class is going use A LOT of these arrays, and in fact they need to be in an array, for indexing purposes and simplicity of programming. However, VB6 will not let you declare public arrays in a class module. I am aware that I could declare an array as a private variables, but then I see no way to reference or directly edit the first class and its elements from "the outside".

Very confused, any help would be appreciated. Let me know if clarification is needed.

8 Years
Discussion Span
Last Post by MrZerocaL
Featured Replies

Instead of Arrays, You can use Collection object or Your own Collection Class. VB6 does not allow to declare Constants, Arrays, User Defined Types as Public.

In a class Module

Option Explicit
Private mMyCollection As Collection

Public Property Get MyCollection() As Collection
   Set MyCollection = mMyCollection
End Property

Public Property Set MyCollection(ByVal vNewValue As Collection)
   Set mMyCollection = MyCollection
End Property

Private Sub Class_Initialize()
   Set mMyCollection = New Collection
End Sub

Private Sub Class_Terminate()
   Set mMyCollection = Nothing
End Sub

Another way Have Array as Private Type and write the property to access the elements

Private mMyArray(10) As String

Public Property Get MyArray(ByVal iI As Integer) As String
   If iI >= 0 And iI <= UBound(mMyArray) Then
      MyArray = mMyArray(iI)
   End If
End Property

Public Property Let MyArray(ByVal iI As Integer, ByVal vNewval As String)
   If iI >= 0 And iI <= UBound(mMyArray) Then
      mMyArray(iI) = vNewval
   End If
End Property

To use the above
I named the class as CMyClass

Dim oRf As New CMyClass
   Dim i As Integer
   Dim sStr As String

   For i = 0 To 11
      oRf.MyArray(i) = "One" & i
   For i = 0 To 11
      sStr = sStr & oRf.MyArray(i)
   MsgBox sStr

Thanks very much for the response! So far, collections seem to be the best alternative. One thing I have noticed is that the "intellisense" typing feature does not work as well with them, since the program does not appear to know what kind of objects are in the collection until they are added at run-time, but that is a minor detail.

There is a slight difference between my issue and the second example of code. In your case, mMyArray is an array of strings. I can see how that would work. However, I would be making an array of objects. If the array is private, I don't see how I could possibly change the properties of these objects from the outside.

If public arrays were allowed, I could write B.A(i).Prop = (whatever), where B is the parent class, and A would be an array of the objects and Prop is some property. But public arrays are not allowed, so I don't see how I could write the above statement, using a private array.


I think I figured it out. I made a property that is defined as one of the objects, then I set it to the specific element in the private array in the Get procedure. I'm new with classes and I didn't think that would expose the properties of the object to the outside, but apparently it does. Thanks for the help.


Yes the problem is solved (for now at least). I think I included too many details in my original post. I said arrays because the first class module I made were basically arrays with a few extra properties. I should have just said objects. Thanks again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.