我理解type
结构是class
函数的祖先。然而,对Type结构进行编码是快速,简单和容易的。因此,我尝试了以下但没有成功。
在VBa中的一个类中工作,我试图将多个变量从内部函数返回到类中的另一个函数。我试图用type
元素做到这一点,但是类函数中存在内部冲突。
我有两个问题:
1.)课程中不允许types
吗?
2.)从类中的函数返回多变量输出的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
上面代码中给出的错误是:未定义用户定义的类型。感谢
编辑:
帮助理解结构。显示如下:
Class
|--Properties
|--Function: CheckPTID
|--Type: checkResult
真正的问题是,如何在不创建新类的情况下直接在类中使用type
函数。
答案 0 :(得分:1)
在VBa中的一个类中工作,我试图将多个变量从内部函数返回到类中的另一个函数。
如果我已理解您的上述评论,那么您正尝试使用本地/模块级别之外的代码。根据上面的@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
请告诉我这对你有用,因为我还没有测试过! :)
答案 1 :(得分:0)
你的叙述说:“我试图将多个变量从内部函数返回到类中的另一个函数。”。因此,我假设您要在类Type
中使用CheckPTID()
(以及only
方法),即您不希望在任何其他模块中使用该类型。
然后声明Private
该类型以及类中返回该类型变量的任何其他函数(如CheckPTID()
那样)
此外,你必须修改你的功能,因为你必须:
通过声明
来初始化checkResult
类型的变量
Dim 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