Problem Passing Byte-Array to a Class's Property
The property is defined in the class with this code:
Code:
Public Property Let PrivateKey(ByRef Prk() As Byte)
The code in a command button defines an array to pass with this code:
Code:
Dim PrKey(39) As Byte
and then passes it to the class's property with this code:
Code:
RSA.PrivateKey = PrKey()
What am I doing wrong? Or is this a bug in VB6 itself?
Re: Problem Passing Byte-Array to a Class's Property
This is not a "bug in VB6" but an attempt to violate a COM rule.
You cannot really have an array Property. If there is a fault in the VB6 compiler it is that this doesn't get flagged on the Property definition in the Class but on attempts to use it.
You have to either use a method call or else you'll need to pass some sort of object type that wraps the array. The easiest way to do this and get auto-coercion is to make these Variant type properties and add type checking logic if required. For example:
RSA.cls
Code:
Option Explicit
Private mPrk() As Byte
Public Property Get PrivateKey() As Variant
PrivateKey = mPrk
End Property
Public Property Let PrivateKey(ByVal Prk As Variant)
If VarType(Prk) <> (vbArray Or vbByte) Then Err.Raise 5
mPrk = Prk
End Property
Form1.frm
Code:
Option Explicit
Private Sub Command1_Click()
Dim PrKey(39) As Byte
PrKey(1) = 255
With New RSA
.PrivateKey = PrKey
MsgBox .PrivateKey(1)
End With
End Sub