Consulting

Results 1 to 13 of 13

Thread: How to Pass an array of implement(ed) object(s) to a sub/function?

  1. #1
    VBAX Regular
    Joined
    Aug 2015
    Posts
    8
    Location

    How to Pass an array of implement(ed) object(s) to a sub/function?

    Class - NamedData
    public property get Name() as string
    end property
    public property let Name(value as string)
    end property
    
    Class - ProperName
    implements NamedData
    private Name as string
    private property get NamedData_Name() as string
        NamedData_Name = Name
    end property
    private property let Name(value as string)
        Name = value
    end property
    public property get Name() as string
        Name = NamedData_Name
    end property
    public property let Name(value as string)
        NameData_Name = value
    end property
    
    ThisDocument
    dim ProperNames(10) as new ProperName
    
    dim sub xMain()
          LoadNamedData(ProperNames)  ' ByRef Argument Type Mismatch !!!
    end  sub
    
    Sub LoadNamedData(oND() as NamedData)
        ' do stuff
    end sub
    Why am I getting the type mismatch when ProperNames is any array of NamedData (by way of implements)? This also doesn't work if it's not an array. But it does work if byval is used but only for non array.
    Last edited by WallyZ; 02-08-2016 at 12:41 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I am not clear what you are trying to do and what error you are getting. You have a property Name defined twice in the ProperName class, which isn't allowed, and I cannot see anything in ProperName that sets up an array.
    ____________________________________________
    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
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub xMain()
       LoadNamedData ProperNames
    End Sub
    
    Sub LoadNamedData(oND)
       for each it in oND
        msgbox it
       next
    End Sub

  4. #4
    VBAX Regular
    Joined
    Aug 2015
    Posts
    8
    Location
    Quote Originally Posted by WallyZ View Post
    'Class - NamedData
    public property get Name() as string
    end property
    public property let Name(value as string)
    end property
    
    
    'Class - ProperName
    implements NamedData
    
    private Name as string
    
    private property get NamedData_Name() as string
        NamedData_Name = Name
    end property
    
    private property let Name(value as string)
        Name = value
    end property
    
    public property get Name() as string
        Name = NamedData_Name
    end property
    
    public property let Name(value as string)
        NameData_Name = value
    end property
    
    
    'ThisDocument
    dim ProperNames(10) as new ProperName
    
    dim sub xMain()
          LoadNamedData(ProperNames)  ' ByRef Argument Type Mismatch !!!
    end  sub
    
    Sub LoadNamedData(oND() as NamedData)
        ' do stuff
    end sub
    Why am I getting the type mismatch when ProperNames is any array of NamedData (by way of implements)? This also doesn't work if it's not an array. But it does work if byval is used but only for non array.
    To Xld : You are correct. My mistake while copying the code by hand for this example. The "private Name as string" should have been private sName as string" and all references to that private variable should be changed accordingly.

    However my question is why can I pass an implementing class by value and not by reference? If passing by reference cannot be used then array of custom classes cannot be passed!

    To Snb : I've come to the same conclusion and that is; passing as a variant is the only method that will work.
    Last edited by SamT; 02-09-2016 at 10:16 AM. Reason: Added White Space and Comment marks

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    ProperNames(10) is a variant() array, that contains instances of 'propername'. It's not a variable of the type 'propername'.
    Since propernames(10) is a variant it can be apsse as someting else.
    So your 'conclusion' is wrong.

  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    ProperNames(10) wasn't a Variant array in the original code, it was an array of type ProperName.
    Be as you wish to seem

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    WalyZ's code is confusing. There are two different Gets that return the same thing and two identical Lets that set different things.

    Question for all inre ProperNames and the NamedData Property: Should it be NamedData_Name or NamedData.Name?
    Dim Temp As ProperName
    Temp.NamedData.Name = "John"
    Temp.NamedData_Name = "Bill"
    Valid?
    Dim NickNames(10) As String
    Valid?
    Public Type Aliases
    RealName As ProperName
    FakeNames(10) As ProperName
    End Type
    
    Dim PerpNames(10) As Aliases
    Last edited by SamT; 02-09-2016 at 10:31 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Maybe something like this (@SamT -- notice the 'old school 'Call' )

    But based on the code shown by the OP, I think using Impliments might be overly complex


    'main standard module
    Option Explicit
    Dim ProperNames(1 To 10) As New ProperName
     
    Sub xMain()
        Dim i As Long
        
        For i = 1 To 10
            ProperNames(i).NamedData_Name = CStr(i)
        Next i
        Call LoadNamedData(ProperNames)
    End Sub
     
    Sub LoadNamedData(oND() As ProperName)
        Dim i As Long
        
        For i = LBound(oND) To UBound(oND)
            MsgBox oND(i).NamedData_Name
        Next i
    End Sub
    
    
    
    
    
    ' Class -NamedData
    Option Explicit
    
    Public Property Get Name() As String
    
    End Property
    
    
    Public Property Let Name(S As String)
    
    End Property
     
    
    
    
    
    
     
    'Class -ProperName
    Option Explicit
    
    Implements NamedData
    
    Private sName As String
    
    Property Get NamedData_Name() As String
        NamedData_Name = sName
    End Property
    
    
    Property Let NamedData_Name(S As String)
        sName = S
    End Property
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    So... UnderScore is the syntax to use with Implemented Classes?
    Dim Temp As ProperName 
    Temp.NamedData_Name = "Bill"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    https://msdn.microsoft.com/en-us/lib...ffice.15).aspx


    I think Chip has the best tutorial on the subject, BUT Interface & Implements are graduate level subjects as far as I'm concerned

    http://www.cpearson.com/Excel/Implements.aspx
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  11. #11
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Quote Originally Posted by SamT View Post
    So... UnderScore is the syntax to use with Implemented Classes?
    Dim Temp As ProperName 
    Temp.NamedData_Name = "Bill"
    Yes, if you declare the variable as ProperName. But the point of using Implements is to be able to declare it as NamedData, then access the properties from that interface:
    Dim Temp As NamedData
    Set Temp = New ProperName
    Temp.Name = "Bill"
    Thus the OP's original code should have been something like:
    Dim ProperNames(10) As NamedData
    
    Sub xMain()
        Dim n                     As Long
        For n = 1 To 10
            Set ProperNames(n) = New ProperName
        Next n
        LoadNamedData ProperNames
    End Sub
     
    Sub LoadNamedData(oND() As NamedData)
         ' do stuff
    End Sub
    in order to be able to pass the array.
    Be as you wish to seem

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Paul,
    Thanks, I saved the page
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    @SamT --


    NP, but Aflatoon's comment is more valid and more in tune with the intention of using 'Impliments' than my example was
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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