Why can't the VBA Me keyword access private procedures in its own module?

后端 未结 5 1997
梦谈多话
梦谈多话 2020-12-11 15:22

I just discovered that the Me keyword cannot access private procedures even when they are inside its own class model.

Take the following code in Class1:



        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 15:39

    In COM, there's a difference between the types of object instances and the types of object variables. In particular, the types of object variables behave as interface types. Every type implements at least one interface (itself), but types may implement other interfaces as well. Such ability is used to fake inheritance.

    In some frameworks, if class Foo has a private member Bar, then any non-null variable of type Foo will hold a reference to some class object which contains that member. The member may not be accessible to any outside code, but it will exist, and can thus be accessed from anywhere within the code for Foo.

    Because COM class-variable types behave like interfaces rather than inheritable class types, however, there's no guarantee that a variable of type Foo will refer to an object which has any of Foo's non-public members. While a compiler could know that Me will always refer to the present object, which will be of actual type Foo, the fact that the only object upon which a private member of Foo could be accessed is Me means that there's no real reason for the compiler to support dot-based dereferencing of private members.

提交回复
热议问题