Consulting

Results 1 to 5 of 5

Thread: Solved: Passing & Returning array to/from Property Get and Let

  1. #1
    VBAX Regular
    Joined
    Jun 2007
    Location
    Moscow
    Posts
    12
    Location

    Solved: Passing & Returning array to/from Property Get and Let

    Hello Ladies and Gentlemen, please give assistance in the following. I have a Class Module, it has some properties (and declared internal variables, that correspond to these properties). One of these internal variables is an array:
    [VBA]Dim A#()[/VBA]
    I want to refer it from a property, as I do with a usual variable:
    [VBA]Property Get X() as Double()
    X = A
    End Property
    Property Let X(newX() as Double)
    A = newX
    End Property
    [/VBA]
    It doesn?t work this way. It says ?can?t assign to array? when I try to either read the property or write it. Of course, I can read and write them element-by-element, passing the item number as a parameter, and looping through property assignment inside the program code. But I want to make it a nicer way, passing array to property and returning it from property.
    Sincerly Yours,
    千歳みどり, FRM

    Member of:
    www.riskofficer.ru, dom.bankir.ru, www.animeforum.ru

  2. #2

    similar problems

    I had similar problems but I could not defeat the system. In the end I had to compromise with functions that passed in data one at a time to an array that was internal to the class. If I wanted the data in the array then I had to reference the data position (like a normal array). The code is:
    '**********************
    Public Property Let sCriterial(iVal As Integer, sCriterialIn As String)

    ReDim Preserve sCriterialArray(1 To iVal) 'iCriterialLength

    iCriterialLength = UBound(sCriterialArray)
    sCriterialArray(iVal) = sCriterialIn 'iCriterialLength

    End Property
    '************************

    Public Property Get sCriterial(iVal As Integer) As String
    ' used to pass arrays out of class module
    sCriterial = sCriterialArray(iVal)
    End Property
    '*************************
    I had to supply the position for the Property let array as well. This works fine for a loop situation but you must be careful for applicaitons outside a loop.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    This works for me

    [vba]

    'Class
    Private maItems

    Public Property Get ClassItems()
    ClassItems = maItems
    End Property

    Public Property Let ClassItems(ary)
    maItems = ary
    maItems(2) = 99
    End Property
    [/vba]

    As a simple example of usage

    [vba]

    Dim cls As Class1
    Dim ary


    Set cls = New Class1
    ary = [{1,2,3}]
    cls.ClassItems = ary
    MsgBox ary(2)
    ary = cls.ClassItems
    MsgBox ary(2)
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Jun 2007
    Location
    Moscow
    Posts
    12
    Location
    IanAgain, xld, thank you!

    I think I will use a sub to assign an array to the internal array. Passing arrays through subs is convenient. And concerning returning array without item number, i will just avoid it. ^_^

    My class will look like this:
    [VBA]'Class
    Dim A#()

    Sub AssingToA(newA#())
    A = newA
    End Sub[/VBA]
    xld, the example you've provided shows the true power of Variant data type. I'm not much familiar with it, maybe I should pay more attention to possibilities it gives. Thank you for the idea!
    Sincerly Yours,
    千歳みどり, FRM

    Member of:
    www.riskofficer.ru, dom.bankir.ru, www.animeforum.ru

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    The big advantage of an array, within a class module, is that as well as being able to pass the arrau wholesale, it is just as simple to get/set a single item within the array. So, although item number may not be appropriate for you here, don't just forget it, it is extremely useful.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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