VBAtypes的类? 替代多个输出variables?
我知道type
结构是class
函数的祖先。 但是编码Type结构是快速,简单和容易的。 因此,我尝试了以下没有成功。
在VBa的类中工作,我试图从一个内部函数返回多个variables到类中的另一个函数。 我试图用type
元素来做到这一点,但是在类函数中有一个内部的冲突。
我有两个问题:1)类是不允许在课堂上? 2.)什么是从一个类中的函数返回多variables输出的一个good practice
方法?
Private Type checkResult status As Boolean errorp As String End Type Function CheckPTID(PTid As String) As checkResult Dim plen As Boolean ' PT length Dim numdash As Boolean ' numbers and dashes Dim titles As Boolean ' Tiles correct ' initials plen = False numdash = False titles = False ' Checks If Len(PTid) = 8 Then plen = True If InStr(PTid, "-") > -1 Then numdash = True If (Left(PTid, 2) = "XP" Or Left(PTid, 2) = "XA") Then titles = True ' output If (plen = False Or numbdash = False Or titles = False) Then CheckPTID.status = False If Not plen Then CheckPTID.errorp = "** Error Name length incorrect:" & PTid If Not numdash Then CheckPTID.errorp = "** Error Name format incorrect:" & PTid If Not titles Then CheckPTID.errorp = "** Error Name titles incorrect:" & PTid Else CheckPTID.status = True CheckPTID.errorp = "N/A" End If End Function
上面代码中给出的错误是:用户定义的types没有定义。 谢谢
编辑:
帮助理解结构。 显示如下:
Class |--Properties |--Function: CheckPTID |--Type: checkResult
真正的问题是,如何在不创build新类的情况下直接在类中使用type
函数。
在VBa的类中工作,我试图从一个内部函数返回多个variables到类中的另一个函数。
如果我已经理解了你的上面的评论,那么你试图使用本地/模块级以外的代码。 根据上面的@Nathan_Sav注释,公开声明一切。 见下文。
Option Explicit Public plen As Boolean ' PT length Public numdash As Boolean ' numbers and dashes Public titles As Boolean ' Tiles correct Public Type checkResult public status As Boolean public errorp As String End Type Public Function CheckPTID(PTid As String) As checkResult 'initials plen = False numdash = False titles = False 'Checks If Len(PTid) = 8 Then plen = True If InStr(PTid, "-") > -1 Then numdash = True If (Left(PTid, 2) = "XP" Or Left(PTid, 2) = "XA") Then titles = True 'output If (plen = False Or numbdash = False Or titles = False) Then CheckPTID.status = False If Not plen Then CheckPTID.errorp = "** Error Name length incorrect:" & PTid If Not numdash Then CheckPTID.errorp = "** Error Name format incorrect:" & PTid If Not titles Then CheckPTID.errorp = "** Error Name titles incorrect:" & PTid Else CheckPTID.status = True CheckPTID.errorp = "N/A" End If End Function
请让我知道这是如何工作,因为我没有testing过! 🙂
你的叙述说: “我试图从内部函数返回多个variables到class级内的另一个function。 。 因此,我假设你只想在你的类中使用Type
(和CheckPTID()
方法),也就是说你不想在其他模块中使用这个types。
然后在你的类中声明Private
types和任何其他函数返回一个该types的variables(如CheckPTID()
)
此外,你的function必须修改,因为你必须:
-
通过声明它来初始化一个
checkResult
types的variablesDim retCheckPTID As checkResult
-
在整个分组中使用它来设置其属性
retCheckPTID.status = False If Not plen Then retCheckPTID.errorp = "** Error Name length incorrect: " & PTid ...
-
最后设置你的函数的返回值
CheckPTID = retCheckPTID End Function
这里是整个代码:
Option Explicit Private Type checkResult status As Boolean errorp As String End Type Private Function CheckPTID(PTid As String) As checkResult Dim plen As Boolean ' PT length Dim numdash As Boolean ' numbers and dashes Dim titles As Boolean ' Tiles correct Dim retCheckPTID As checkResult ' initials plen = False numdash = False titles = False ' Checks If Len(PTid) = 8 Then plen = True If InStr(PTid, "-") > -1 Then numdash = True If (Left(PTid, 2) = "XP" Or Left(PTid, 2) = "XA") Then titles = True ' output If (plen = False Or numdash = False Or titles = False) Then retCheckPTID.status = False If Not plen Then retCheckPTID.errorp = "** Error Name length incorrect: " & PTid If Not numdash Then retCheckPTID.errorp = "** Error Name format incorrect:" & PTid If Not titles Then retCheckPTID.errorp = "** Error Name titles incorrect: " & PTid Else retCheckPTID.status = True retCheckPTID.errorp = "N/A" End If CheckPTID = retCheckPTID End Function