Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Objects and UDT arrays

I have created a User Defined Type called Satus, I then created two arrays WStatus and BStatus. I would like to refer to one of the two arrays with an object CStatus. Is this possible with Visual Basic 6?

Sample Code

Dim Wstatus(17) As Status
Dim Bstatus(17) As Status
Dim CStatus As Object

Private Sub Form_Load()
     Set CStatus = Bstatus ' this is where I get the error "object required"

End Sub

I have also tried Set CStatus = Bstatus(), and when I tried CStatus = Bstatus that gave me "Compile Error: Only User-defined tupes defined in public object modules can be coerced to or from a variant or passed to late-bound functions"

Any help would be appriciated.
1 Solution
Instead of using a UDT you could create a class module.

Here is an example UDT:

Type SampleUDT
    MyField1 As String
    MyField2 As Long ' etc.
End Type

Add a class module like this:

Option Explicit

Public MyField1 As String
Public MyField2 As Long

Now if you want an array of objects you can either use a variant array and place an object in each element.

Or use a collection.

Dim MyCollection As New Collection

Dim MyStat As MyClass
Set MyStat = New MyClass

MyStat.MyField1 = "....."
MyStat.MyField2 = 1234

MyCollection.Add MyStat

Here is a very easy suggestion for you:-

instead of these lines:

Dim Wstatus(17) As Status
Dim Bstatus(17) As Status
Dim CStatus As Object

Just substitute these:

Dim BothStatuses(1,17) As Status
Dim CStatus As Integer

You can now read your status as follows

   BothStatuses(CStatus, N)

When CStatus is "0" you will get one set of values, when it is "1" you will get the other set.

This is called a two-dimensional array.

I hope this helps

optikonAuthor Commented:
Thanks, I had considered a 2d array, but I never thought of using one of the diminsions as a flag, thanks.

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now