处理循环引用问题的提示 [英] Tip for dealing with circular reference issue

查看:67
本文介绍了处理循环引用问题的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚提出了一个非常整洁的小解决方案来解决VB / VBA循环

参考问题。它只适用于Access 2000或更新版本,但这只是

唯一不利的一面。


问题......

您需要一个包含容器对象的对象模型,以及容器需要与容器本身交互的对象。问题是

,为了访问容器,每个项目都需要对容器的引用,

并且构成循环引用,因为容器还包含

数组或包含对包含对象的引用的集合。

除非使用极端谨慎,否则此结构可以保留在内存中,即使所有内容都是b $ b外部人员对它的引用已超出范围,每次创建一个新内存时,内存将逐渐被吞噬。


从Access 2000开始,有一个工具似乎处理这个,这是创建和引发自定义事件与引用

对象进行通信的能力,但这仅适用于单个引用,不是数组或集合。


解决方案......

创建一个通用代理类,可以被容器和
$引用b $ b包含可以被要求将事件提交到容器以询问f的项目或者

a临时引用容器以便在单个函数调用中使用。


clsContainerAccessor

======= ===

选项明确

选项比较数据库


事件请求参考(ByRef objReference As Object)


公共属性获取引用()作为对象

Dim objReference As Object

RaiseEvent RequestReference(objReference)

Set Reference = objReference

结束财产

==========


在contianer中,添加这样的代码。 ..

==========

Private WithEvents mobjAccessor as clsContainerAccessor


Private Sub mobjAccessor_RequestReference(objReference)作为对象)

设置objReference =我

结束子


私有子Class_Initialize()

设置mobjAccessor =新clsContainerAccessor

结束子

==========


当容器创建一个NE w包含项目,它可以为项目提供一个对mobjAccessor的

引用,并且该项目可以通过访问该对象的Reference属性获得对容器的临时引用

根据需要。

解决方案



" Steve Jorgensen" <无**** @ nospam.nospam>在消息中写道

新闻:6k ******************************** @ 4ax.com ...

我刚刚提出了一个非常整洁的小解决方案来解决VB / VBA循环
参考问题。它只适用于Access 2000或更新版本,但只有
关于唯一不足的问题。

问题......
你需要一个包含一个对象的对象模型容器对象,容器中的对象
需要与容器本身进行交互。问题是,为了访问容器,每个项目都需要对
容器的引用,这构成了一个循环引用,因为容器也是
包含一个数组或一个包含一个对包含对象的引用。
除非使用极端谨慎,否则这个结构可以将自己保存在内存
中,即使所有外部引用都超出了范围,并且内存
将逐渐被吞噬每次创建一个新的。




你如何假设Me.Parent在不吞咽内存的情况下工作?


它没有缓存实例 - 对父进行弱引用,并根据需要检索

对象。

-

MichKa [MS]

NLS整理/现场/键盘开发

全球化基础设施和字体技术


该帖子按原样提供。

无保证,并且不授予任何权利。

" rkc" < rk*@yabba.dabba.do.rochester.rr.nope>在消息中写道

新闻:Qt ******************* @ twister.nyroc.rr.com ...


Steve Jorgensen <无**** @ nospam.nospam>在消息中写道
新闻:6k ******************************** @ 4ax.com ... < blockquote class =post_quotes>我刚刚提出了一个非常整洁的小解决方案来解决VB / VBA循环
参考问题。它只适用于Access 2000或更新版本,但这是关于

唯一不利的问题。

问题......
你需要一个对象包含容器对象的模型,以及

中的
对象,容器需要与容器本身进行交互。问题



是,要访问容器,每个项目都需要引用


容器,

并构成一个循环引用,因为容器


包含一个

数组或一个包含对所包含对象的引用的集合。
除非极端谨慎使用过,这个结构可以将自己保存在内存中


甚至

虽然所有对它的外部引用都超出范围,而内存


会< blockquote class =post_quotes>每次创建一个新的时候逐渐被吞噬。



你如何假设Me.Parent在不吞咽记忆的情况下工作?





" Steve Jorgensen" <无**** @ nospam.nospam>在消息中写道

新闻:6k ******************************** @ 4ax.com ...

我刚刚提出了一个非常整洁的小解决方案来解决VB / VBA循环
参考问题。它只适用于Access 2000或更新版本,但只有
关于唯一的下方。




< snip>


您的帖子和M.Kaplan对我的me.parent问题的回复使我

花了我的上午早上阅读。


这是我发现的最有趣的文章:
http://www.programmersheaven.com/art...y/article1.htm


另外
http://msdn.microsoft .com / library / de ... references.asp



http://msdn.microsoft.com/library/de...ctlifetime。 ASP


这就是我实施解决方案的方式。

我能得到它吗?


< ;员工类>

私人姓名_作为字符串

私人部门_作为访问者


公共财产让名称(n为字符串)< br $>
Name_ = n

结束财产


公共财产获取名称()为字符串

名称=姓名_

结束物业


公共财产集团部门(作为配件)

设置部门_ = acc

结束财产


公共财产获取部门()为字符串

部门=部门_参考。名称

结束财产

< /员工类>


<部门类>

公共名称为字符串

公共员工作为新集合

Private WithEvents mobjAccessor作为访问者


Public Sub AddEmployee(ByVal strName As String)

Dim e As员工


设置e =新员工

e.Name = strName

设置e.Department = mobjAccessor

Employees.Add e


设置e = Nothing

End Sub


私有子mobjAccessor_RequestReference(objReference As Object)

设置objReference = Me

结束子


私人子类_初始化()

设置mobjAccessor =新的访问者

结束子

< /部门类>


I just came up with a really tidy little solution to the VB/VBA circular
reference issue. It only works with Access 2000 or newer, but that''s about
the only down-side.

The issue...
You need an object model that includes a container object, and the object in
the container need to interact with the container itself. The problem is
that, to access the container, each item needs a reference to the container,
and that constitutes a circular reference since the container also contains an
array or a collection that contains a reference to the contained object.
Unless extreme caution is used, this structure can hold itself in memory even
though all externalr references to it have gone out of scope, and memory will
gradually be swallowed up each time a new one is created.

As of Access 2000, there is a tool that seems to handle this which is the
ability to create and raise custom events to communicate with a referring
object, but this only works for single references, not arrays or collections.

The solution...
Create a generic proxy class that can be referenced by both a container and a
contained item that can be asked to raise an event to the container to ask for
a temporary reference to the container for use within a single function call.

clsContainerAccessor
==========
Option Explicit
Option Compare Database

Event RequestReference(ByRef objReference As Object)

Public Property Get Reference() As Object
Dim objReference As Object
RaiseEvent RequestReference(objReference)
Set Reference = objReference
End Property
==========

In the contianer, add code like this...
==========
Private WithEvents mobjAccessor As clsContainerAccessor

Private Sub mobjAccessor_RequestReference(objReference As Object)
Set objReference = Me
End Sub

Private Sub Class_Initialize()
Set mobjAccessor = New clsContainerAccessor
End Sub
==========

When the container creates a new contained item, it can supply the item with a
reference to mobjAccessor, and the item can obtain a temporary reference to
the container by accessing the Reference property of that object as needed.

解决方案


"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:6k********************************@4ax.com...

I just came up with a really tidy little solution to the VB/VBA circular
reference issue. It only works with Access 2000 or newer, but that''s about the only down-side.

The issue...
You need an object model that includes a container object, and the object in the container need to interact with the container itself. The problem is
that, to access the container, each item needs a reference to the container, and that constitutes a circular reference since the container also contains an array or a collection that contains a reference to the contained object.
Unless extreme caution is used, this structure can hold itself in memory even though all externalr references to it have gone out of scope, and memory will gradually be swallowed up each time a new one is created.



How do you suppose Me.Parent works without swallowing up memory?


It does not cache an instance -- there is a weak ref to the parent and the
object is retrieved as needed.
--
MichKa [MS]
NLS Collation/Locale/Keyboard Development
Globalization Infrastructure and Font Technologies

This posting is provided "AS IS" with
no warranties, and confers no rights.
"rkc" <rk*@yabba.dabba.do.rochester.rr.nope> wrote in message
news:Qt*******************@twister.nyroc.rr.com...


"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:6k********************************@4ax.com...

I just came up with a really tidy little solution to the VB/VBA circular
reference issue. It only works with Access 2000 or newer, but that''s about

the only down-side.

The issue...
You need an object model that includes a container object, and the object in

the container need to interact with the container itself. The problem


is that, to access the container, each item needs a reference to the


container,

and that constitutes a circular reference since the container also


contains an

array or a collection that contains a reference to the contained object.
Unless extreme caution is used, this structure can hold itself in memory


even

though all externalr references to it have gone out of scope, and memory


will

gradually be swallowed up each time a new one is created.



How do you suppose Me.Parent works without swallowing up memory?




"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:6k********************************@4ax.com...

I just came up with a really tidy little solution to the VB/VBA circular
reference issue. It only works with Access 2000 or newer, but that''s about the only down-side.



<snip>

Your post and M.Kaplan''s reply to my me.parent question caused me to
spend my hung-over morning reading up on this.

This was the most interesting article I found:
http://www.programmersheaven.com/art...y/article1.htm

Also
http://msdn.microsoft.com/library/de...references.asp

and
http://msdn.microsoft.com/library/de...ctlifetime.asp

This is how I implemented your solution.
Do I get it?

<Employee class>
Private Name_ As String
Private Department_ As Accessor

Public Property Let Name(n As String)
Name_ = n
End Property

Public Property Get Name() As String
Name = Name_
End Property

Public Property Set Department(acc As Accessor)
Set Department_ = acc
End Property

Public Property Get Department() As String
Department = Department_.Reference.Name
End Property
</Employee class>

<Department class>
Public Name As String
Public Employees As New Collection
Private WithEvents mobjAccessor As Accessor

Public Sub AddEmployee(ByVal strName As String)
Dim e As Employee

Set e = New Employee
e.Name = strName
Set e.Department = mobjAccessor
Employees.Add e

Set e = Nothing
End Sub

Private Sub mobjAccessor_RequestReference(objReference As Object)
Set objReference = Me
End Sub

Private Sub Class_Initialize()
Set mobjAccessor = New Accessor
End Sub
</Department class>


这篇关于处理循环引用问题的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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