在VBA中,有没有任何已知的机制来欺骗编译器允许使用保留关键字作为类属性的名称?例如,我想创建一个Select
在我的一个类模块中调用的属性.但是,编译器将我的声明标记为错误.以下是我使用的语法:
Public Property Get Select() As Database.SQLStatements
End Property
Run Code Online (Sandbox Code Playgroud)
Database
是我的VBA项目名称,SQLStatements
是我创建的类模块之一.此外,我正在MS Access 2010中运行代码.
您可以这样做,并在您的VBA中使用任何关键字/保留字.但是它会使你的代码变得非常混乱,很难读取/调试/维护.
如果你有一个If
在你的班级中命名的bool属性,你最终会得到这样的东西 If .If Then
,好吧,祝你好运.此外,代码维护,如查找/替换/重命名等,将需要格外小心和更多的工作.
无论如何,如果你愿意经历所有这些痛苦,这就是你如何做到的.
在关键字/保留字之后使用ALT + 0160添加一个不可见的空格 ,就是这样.VBA将其视为完全合法的名称.例如If
.
此外,您必须使用intellisense来使用这些属性名,或者在任何地方手动键入altcode.这是额外的打字.
另clsTest
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
Run Code Online (Sandbox Code Playgroud)
测试模块
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
Run Code Online (Sandbox Code Playgroud)
ALT + 0160 <>空间