View Full Version : How to Pass an array of implement(ed) object(s) to a sub/function?
WallyZ
02-07-2016, 11:59 PM
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.
Bob Phillips
02-08-2016, 03:40 AM
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.
Sub xMain()
LoadNamedData ProperNames
End Sub
Sub LoadNamedData(oND)
for each it in oND
msgbox it
next
End Sub
WallyZ
02-08-2016, 05:02 PM
'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.
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.
Aflatoon
02-09-2016, 01:28 AM
ProperNames(10) wasn't a Variant array in the original code, it was an array of type ProperName.
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
Paul_Hossler
02-09-2016, 11:30 AM
Maybe something like this (@SamT -- notice the 'old school 'Call' :devil2:)
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
So... UnderScore is the syntax to use with Implemented Classes?
Dim Temp As ProperName
Temp.NamedData_Name = "Bill"
Paul_Hossler
02-09-2016, 12:46 PM
https://msdn.microsoft.com/en-us/library/office/gg264387(v=office.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
Aflatoon
02-10-2016, 01:34 AM
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.
Paul,
Thanks, I saved the page :)
Paul_Hossler
02-10-2016, 10:29 AM
@SamT --
NP, but Aflatoon's comment is more valid and more in tune with the intention of using 'Impliments' than my example was
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.