gpt4 book ai didi

类中的 VBA 类型?多个输出变量的替代品?

转载 作者:行者123 更新时间:2023-12-04 20:37:11 46 4
gpt4 key购买 nike

我了解 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直接在类中运行,无需创建新类。

最佳答案

Working inside a class in VBa, I tried to return multiple variables from an internal function to another function within the class.



如果我理解了您的上述评论,那么您正在尝试使用本地/模块级别之外的代码。根据上面的@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

请让我知道这对您有用,因为我还没有测试过! :)

关于类中的 VBA 类型?多个输出变量的替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40587571/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com