Seth Kania
Seth Kania

Reputation: 251

VBA - Store an array inside of a class - Type mismatch

I've been searching all over but nothing seems to do the trick for me. Here is the problem:

I want to store an array of "keys" Here is my simple class:

Private pkeys_length As Integer
Private pkeys() As String
Public Property Get keys_length() As Integer
    keys_length = pkeys_length
End Property
Public Property Let keys_length(arg As Integer)
    pkeys_length = arg
End Property
Public Property Get Keys() As String
    Keys = pkeys()
End Property
Public Property Let Keys(ByVal arg As String)
    ReDim pkeys(0 To pkeys_length) As String
    pkeys = arg
End Property

And here is what I am trying to store:

Dim str_pkeys() As String
Dim pkey_count As Integer
pkey_count = CountPrimaryKeys(stbl)
'Store the keys of that table
ReDim str_pkeys(pkey_count) As String
keyset_1.keys_length = pkey_count
str_pkeys = FindPrimaryKeys(keyset_1.Table)
keyset_1.Keys = str_pkeys

As it stands, it Gives the error Compile Error: Type mismatch I have had several problems while storing the array, I'm not sure if I am actually getting anywhere. This is the only error I haven't been able to fix. All I need to be able to do is store the array of strings in the class.

Anyone familiar with how to go about this?

Upvotes: 1

Views: 689

Answers (1)

Peter Albert
Peter Albert

Reputation: 17475

I think you need to use String() in your Get property and remove the ByVal in the Let:

Private pkeys_length As Integer
Private pkeys() As String

Public Property Get keys_length() As Integer
    keys_length = pkeys_length
End Property

Public Property Let keys_length(arg As Integer)
    pkeys_length = arg
End Property
Public Property Get Keys() As String()
    Keys = pkeys
End Property

Public Property Let Keys(arg() As String)
    ReDim pkeys(0 To pkeys_length) As String
    pkeys = arg
End Property

Apart from this a small design suggestion: do you really need a Set for the array length? Why not include this in the set of the array - and only provide the Get instead?

Upvotes: 1

Related Questions