VBA [classes]-如何访问另一个子类中某个实例的先前实例化的实例 [英] VBA [classes] - how to access a previously instanciated instance of a class in another sub

查看:69
本文介绍了VBA [classes]-如何访问另一个子类中某个实例的先前实例化的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在程序的特定子目录中创建了狗"类的实例"Lassie".我还给了它一些有用的属性,例如Lassie.Age = 7和Lassie.HeelCapability = False.

I've created the instance "Lassie" of the class "Dog" within a particular sub in my program. I have also given it some useful properties, such as Lassie.Age = 7 and Lassie.HeelCapability = False.

现在,我想访问另一个子项,并将Lassie.HeelCapability更改为True. 我该如何在这个新的子项中定义实例"Lassie",以便可以使用它?

Now I would like to access another sub and change the Lassie.HeelCapability to True. How do I go about defining the instance "Lassie" within this new sub so it can be worked with?

我所遇到的所有代码都是这样的:

All the code I have come accross goes like this:

Dim Lassie As classDog

Set Lassie = New classDog

基本上,我要寻找的是一种将现有实例"Lassie"导入另一个子目录的方法,而无需使用关键字"New",从而创建一个新的"Lassie"(没有前面给出的所有属性).

Essentially what I am looking for is a way to import the existing instance "Lassie" into another sub, without using the keyword "New" and thereby creating a new "Lassie" (without all the properties previously given).

我收到的错误消息告诉我需要对象"或对象变量或未设置块变量".

The errormessages I have been receiving tell me either "object required" or "object variable or with block variable not set".

当然可以做到这一点.

谢谢.

推荐答案

您需要将"Lassie"作为参数传递给其他子对象.

You will need to pass 'Lassie' as a parameter to your other sub.

public sub DeclareSub()
  Dim Lassie as classDog
  Lassie = new classDog

  OtherSub Lassie
end sub

public sub OtherSub(ByRef dog as classDog) 

end sub

子例程"OtherSub"中的变量"dog"与"DeclareSub"中的变量"Lassie"引用相同的对象实例.

The variable 'dog' in the subroutine 'OtherSub' refers to the same object instance as the variable 'Lassie' from 'DeclareSub'.

这篇关于VBA [classes]-如何访问另一个子类中某个实例的先前实例化的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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