Consulting

Results 1 to 4 of 4

Thread: Solved: passing an array to a class

  1. #1
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location

    Solved: passing an array to a class

    Hi,

    I am looking at creating my first class. Instead of assigning each property individually is there a way for me to pass it an array and have those properties set from the array.

    So instead of
    myclass.property1 = "asdf"
    myclass.property2 = "safd"
    myclass.property3 = 1234

    I just pass it the array of "asdf", "safd", 1234 and it assigns those properties automatically?

    Thanks
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You could do something like,

    [vba]

    'caller code

    myclass.SetArray ("asdf", "safd", 1234")


    'class code

    Private mProperty1 As String
    Private mProperty2 As String
    Private mProperty3 As Long

    Public Property Get Property1 As String
    Property1 = mProperty1
    End Property

    Public Property Get Property2 As String
    Property2 = mProperty2
    End Property

    Public Property Get Property3 As Long
    Property3 = mProperty3
    End Property

    Public Function SetArray(ary As Variant)
    mProperty1 = ary(1)
    mProperty2 = ary(2)
    mProperty3 = ary(3)
    End Function[/vba]

    although I wouldn't, I would stick with the normal approach
    ____________________________________________
    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

  3. #3
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    Quote Originally Posted by xld
    although I wouldn't, I would stick with the normal approach
    Any particular reason?
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because it just looks and feels more 'right' to me, properties are individual attributes, so pass them individually.
    ____________________________________________
    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
  •