Passing objects to procedures in VBA
I am working on a simple tool that开发者_运维问答 will allow me to parse multiple CSV files and spit them out onto a fresh worksheet "merged" together. Here is my implementation (I've simplified it) and my issue:
Class A
private variables as types
property methods for accessing variables
Class B
private variables as types
property methods for accessing variables
Class C
Private cA as ClassA
Private cB as Collection 'Collection of ClassB
Class D - Part of my problem
Private cC as Collection 'Collection of ClassC
'Other member variables and their property get/lets
Public Sub AddA(A as ClassA)
If cC.Item(A.foo) is Nothing then
dim tempC as ClassC
set tempC = new ClassC
tempC.A = A
End if
End Sub
Main Module - Other half of my problem
Dim cC as New ClassC
'Initialize Class C, this all works fine
Dim tempA as ClassA
Set tempA = new ClassA
'Set tempA properties
cC.AddA tempA 'This is where my error is
I've tried passing it as ByVal
and ByRef
each gives me different errors ("byref argument type mismatch", "invalid procedure or argument", and "Object doesn't support this property or method"
I have no idea what to try next, I even tried the parenthesis "thing" that supposedly forces the parameter into either ByVal or ByRef, I can't remember, that was yesterday.
Thanks.
This line:
tempC.A = A
means "assing to A
property of tempC
object the value of the default property of the A
object."
Your A
object apparently doesn't have a default property.
What you actually meant was probably:
Set tempC.A = A
But even then, you can't access a private field A
of C
class from D
class. Make the field public or create a public SetA()
method on C
class and call it from D
.
精彩评论