嵌套有语句层次结构 [英] Nested With Statement hierarchy

查看:91
本文介绍了嵌套有语句层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到过几次,只是很好奇是否有更简单的方法来做...

I've come across this a couple of times recently and was just curious if there was an easier way to do this...

With Activeworkbook
  'Do Stuff
  With .Sheets(1)
    'More stuff Done
    '...
    'But now I need to refer to or pass the Sheet in the last With Statement
    SomeFunctionAnswer = SomeFunction Activeworkbook.Sheets(1)
  End With
  'Yet more stuff Done
End With

是否必须将其完全写出,还是有某种方法可以使其变得更容易/更清洁?也许有某种属性或方法可以仅针对此实例进行传递?从更高的With引用属性或方法呢?

Does it have to be fully written out, or is there some way of notating it that makes it easier/cleaner? Perhaps there is some sort of property or method to pass itself for just this instance? What about referring to a property or method from the higher With?

SomeFunctionAnswer =  SomeFunction .Self  '???
'OR
SomeFunctionAnswer =  SomeFunction .Parent.Name  '???

希望如此...

推荐答案

答案是简单明了的否定号.

The answer is a plain and simple No No.

With子句有助于访问其主题的成员和方法,但是它不提供任何引用主题本身的便利.必要时,您必须完全写出对象的名称或通过其他方式引用它.

The With clause facilitates the access to the members and methods of its subject, but it does not provide any facility to reference the subject itself. When that's needed, you have to write the name the object completely or refer to it by other means.

再次访问作为 outer With子句主题的对象的方法和成员时,同样需要完全命名.内部With及其整个作用域完全隐藏了外部With.

When accessing methods and members of an object that is the subject of an outer With clause, again, you need to name it completely. The inner With, and for the whole of its scope, completely hides the outer With.

因此,编写代码的方式才是正确的方式.

Therefore, the way you wrote your code is the correct way to do it.

这篇关于嵌套有语句层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆