【问题标题】:Can a with-block variable be referenced by a keyword in VBA?VBA中的关键字可以引用with-block变量吗?
【发布时间】:2018-09-30 21:26:37
【问题描述】:

with-block 变量是否有关键字?例如,如果该关键字是 This,则以下代码将起作用:

With New myType
    .DoSomething
    DoSomethingElse "abc", This, 123
End With

【问题讨论】:

    标签: vba with-statement


    【解决方案1】:

    恐怕这样的关键字不存在。在 Visual Basic 中,我为此编写了一个扩展方法 .Myself。在 VBA 中我不知道任何解决方案。当然你可以使用局部变量。

    Set a = New mytype
    a.DoSomething
    DoSomethingElse "abc", a, 123
    

    【讨论】:

      【解决方案2】:

      您可以将以下 Function 添加到您的 MyType 类中

      Public Function This() As MyType
          Set This = Me
      End Function
      

      以便您的主要代码可以如下利用它:

      With New MyType
          .DoSomething
          DoSomethingElse "abc", .This, 123
      
      End With
      

      【讨论】:

      • 相当有趣和聪明。不那么匿名的匿名实例。 :)
      • @SMeaden,一个相当“有趣但有趣”的定义
      猜你喜欢
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 2015-08-11
      相关资源
      最近更新 更多