首页 > 解决方案 > VBA:将类型名称作为参数传递(Inventor iLogic)

问题描述

我想创建带有类型检查的函数。

function MyFunc(ByVal Obj As Object, Typ As Type) As Boolean
  ...
  If TypeOf Obj Is typ Then
  ...
End Function

对于标准类型,比较返回的字符串没有问题,TypeName(Obj)如下所述:2010 VBA - Pass Type as parameter

但这在 iLogic 中很棘手。有主要类型Document和派生的“子类型” AssemblyDocumentDrawingDocument...

这些子类型中的每一个也是Document类型。出于我的目的,我对它是什么 Document 子类型不感兴趣。我只需要检查主要Document类型。

更好地解释一些代码:

Dim Obj As Document
Obj = ThisDoc.Document

Logger.Debug(TypeName(Obj)) ' <-- Returns: AssemblyDocument
Logger.Debug(TypeOf Obj Is Document) ' <-- Returns: True

Logger.Debug(TypeName(ThisDoc.Document)) ' <-- Returns: AssemblyDocument
Logger.Debug(TypeOf ThisDoc.Document Is Document) ' <-- Returns: True

那么,有没有办法将类型传递给函数,或者是获取可能子类型列表并将它们逐项与字符串结果进行比较的唯一方法TypeName(Obj)

标签: vbatypes

解决方案


推荐阅读