将对象传递给VBA中的过程 [英] Passing objects to procedures in VBA
问题描述
我正在使用一个简单的工具,让我解析多个CSV文件,并将它们吐出一个合并的新工作表。这是我的实现(我已经简化了)和我的问题:
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:
A类
private variables as types
property methods for accessing variables
B类
private variables as types
property methods for accessing variables
C类
Private cA as ClassA
Private cB as Collection 'Collection of ClassB
D类 - 我的问题的一部分
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
我尝试将其作为 ByVal
和 ByRef
每个都给我不同的错误(byref参数类型不匹配,无效过程或参数,对象不支持此属性或方法
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"
我不知道接下来要尝试什么,我甚至尝试了这个括号中的事情,据说这个参数会强制参数到ByVal或ByRef,我不记得了,这是昨天。
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.
谢谢。
推荐答案
/ p>
This line:
tempC.A = A
表示贬值到
属性对象的默认属性值 tempC
的 A
对象。
您的 A
对象显然没有默认的p
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.
您实际上意味着什么:
Set tempC.A = A
但即使如此,您也无法访问私人字段 D
类中的 C
类将该字段公开或在 C
类上创建公共 SetA()
方法,并从ð
。
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
.
这篇关于将对象传递给VBA中的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!