Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA-名称为保留关键字的类属性_Vba - Fatal编程技术网

VBA-名称为保留关键字的类属性

VBA-名称为保留关键字的类属性,vba,Vba,在VBA中,是否存在任何已知的机制来欺骗编译器允许使用保留关键字作为类属性的名称?例如,我想在我的一个类模块中创建一个名为Select的属性。但是,编译器将我的声明标记为错误。下面是我使用的语法: Public Property Get Select() As Database.SQLStatements End Property Database是我的VBA项目名称,SQLStatements是我创建的类模块之一。另外,我正在MS Access 2010中运行代码。您可以这样做,并在VBA

在VBA中,是否存在任何已知的机制来欺骗编译器允许使用保留关键字作为类属性的名称?例如,我想在我的一个类模块中创建一个名为
Select
的属性。但是,编译器将我的声明标记为错误。下面是我使用的语法:

Public Property Get Select() As Database.SQLStatements

End Property

Database
是我的VBA项目名称,
SQLStatements
是我创建的类模块之一。另外,我正在MS Access 2010中运行代码。

您可以这样做,并在VBA中使用任何关键字/保留字。但是这样会使代码变得非常混乱,并且很难阅读/调试/维护

如果你在你的类中有一个名为
If
的bool属性,你最终会得到类似这样的
If.If-Then
,祝你读到它好运。此外,代码维护,如查找/替换/重命名等,将需要额外的注意和更多的工作


不管怎样,如果你愿意经历所有这些痛苦,下面是你如何做到的。 在关键字/保留字之后,使用ALT+0160添加一个不可见的空格。VBA会认为这是一个完全合法的名字。e、 g.
如果

此外,您必须使用intellisense来使用这些propertynames,或者在任何地方手动键入altcode。那是额外的打字


克莱斯特

Option Explicit

Private m_sSelect   As String
Private m_bIF       As Boolean

Public Property Get Select () As String '~~> Select () is actually typed as SelectALT+0160()
    Select  = m_sSelect
End Property

Public Property Let Select (ByVal sNewValue As String)
    m_sSelect = sNewValue
End Property

Public Property Get If () As Boolean
    If  = m_bIF
End Property

Public Property Let If (ByVal bNewValue As Boolean)
    m_bIF = bNewValue
End Property

测试模块

Option Explicit

Sub demo()

    Dim objTestClass As clsTest

    Set objTestClass = New clsTest

    With objTestClass
        .Select  = "It works. But it will, for sure, create readibility/maintenance issues."
        .If  = False
    End With

    MsgBox objTestClass.Select 

    '/ See how hard it will to read/debug this sort of code

     With objTestClass
        If .If  Then '~~> This line here  :)
            MsgBox "If prop value is TRUE"
        Else
            MsgBox "If prop value is FALSE"
        End If
     End With

End Sub


ALT+0160空格

您需要的搜索是“vba使用保留字作为标识符”,它给出,表示用方括号括住标识符。感谢您给了我一个很好的解决方法。我对额外的空间不太感兴趣,但它绝对有效,我学到了一些新的东西。