Results 1 to 2 of 2

Thread: Problem Passing Byte-Array to a Class's Property

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,238

    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?

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width