UDT可以以任何方式用作方法参数吗? [英] Can UDT's be used as method parameters in any way?
问题描述
多年来,我一直在避免在VBA中使用 Public Type
UDT,因为它们很难传递,我从来没有试图理解为什么。直到现在 - 它只是更容易创建一个类模块,并处理实际的对象。
For years I've been avoiding the use of Public Type
UDT's in VBA, because they're hard to pass around and I never really bothered trying to understand why.. until now - it was simply easier to just create a class module and work with actual objects instead.
但最近我给了一个镜头,一旦我想他们有要通过 ByRef
(作为数组),事情开始看起来像我可以开始使用它们。
But recently I gave it a shot, and once I figured they had to be passed ByRef
(as an array would), things started to look like I could start using them.
所以我在一个标准模块中定义了一个 Public Type
,得到这个编译错误:
So I defined a Public Type
in a standard module, got this compile error:
所以我把
进入类模块,创建类 PublicNotCreatable
,然后得到此编译错误:
So I moved the Public Type
into a class module, made the class PublicNotCreatable
, and then got this compile error:
编译错误。
Option Explicit
' cannot define a public user-defined type within an object module
Public Type TSomething
Foo As Integer
End Type
Public Function Create(ByRef info As TSomething) As Something
End Function
如果移动 TSomething
到标准模块,你会得到其他编译器错误,告诉你公共UDT必须在公共对象模块中定义
If you move the definition of TSomething
to a standard module, you'll get the other compiler error, telling you that the public UDT must be defined in a public object module (i.e. a class module)... which takes you back to square one.
所以如果你不能定义一个 Public Type
在类模块中,为什么编译器会抛出一个契合,甚至提及在公共对象模块中定义的公共用户定义的类型 't合法存在?
So if you cannot define a Public Type
in a class module, why would the compiler throw a fit and even mention "public user defined types defined in public object modules" if such a thing can't legally exist?
它在VB6中工作,编译器消息是该版本的残余吗?或者是COM工作原因是什么?它只是我或两个错误消息彼此矛盾吗?还是有一些我不明白的地方?
Did it work in VB6 and the compiler message is a remnant of that version? Or is the reason somewhere in how COM works? Is it just me or the two error messages are contradicting each other? Or there's something I'm not understanding?
显然我在这里滥用/滥用UDT。
Obviously I'm misusing/abusing UDT's here. So what are they supposed to be used for, if not for passing a "record" to some method?
推荐答案
从标准模块它工作没有任何错误。以下代码不会出错。
From standard module it works without any error. Following code threw no error.
Public Type TEST_TYPE
Prop1 As String
End Type
Public Function fTest(ByRef param1 As TEST_TYPE) As String
param1.Prop1 = "Hello from function"
End Function
Public Sub sTest(ByRef param1 As TEST_TYPE)
param1.Prop1 = "Hello from Sub"
End Sub
Public Sub caller()
Dim p As TEST_TYPE
'/Call Sub
Call sTest(p)
MsgBox p.Prop1
'/Call Function
Call fTest(p)
MsgBox p.Prop1
End Sub
UDT的一个问题是关于转发引用。
One issue with UDT is about Forward referencing. So this will not compile, apart from that It works perfectly fine with standard modules.
Public Type TEST_TYPE
Prop1 As String
Prop2 As TEST_TYPE2 '/ Fails due to Forward referencing. TEST_TYPE2 should be declared before this UDT.
End Type
Public Type TEST_TYPE2
Prop3 As String
End Type
编辑:
但是,在类中使用UDT的工作是 Friend
However, the work around to use the UDT in class is Friend
类别的VBA代码
'/ Using UDT in VBA-Class
Private Type TEST_TYPE3
Prop3 As String
End Type
Public Sub caller()
Dim p As TEST_TYPE3
p.Prop3 = "Hello from Class"
Call testClassUDT(p)
End Sub
Friend Sub testClassUDT(p As TEST_TYPE3)
MsgBox p.Prop3
End Sub
这篇关于UDT可以以任何方式用作方法参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!