circular references

  • Thread starter Stephen Robertson
  • Start date
S

Stephen Robertson

We are currently in a dead end with a circular reference issue using
vb.net, and are hoping someone might help us resolve it.

Idea...

We have frmmain calling frmperson (dim f as new frmperson) in search
(no record) mode. When the search is executed, frmperson calls
frmsearchresult (dim f as new frmsearchresult) which is a listing of
persons. From frmsearchresults, frmperson is called (dim f as new
frmperson) with the resulting record.

Problem...

We will get a circular reference between frmperson and
frmsearchresult.

I have read the mention of events as a workaround. Could someone
please elaborate on how the use of events could possibly help us in
our situation?

Or, is there another way?

Thank you very much,

Dawn Robertson
 
R

Rob Teixeira [MVP]

First of all, i'm not really seeing a circular reference here. you have two
independant instances of frmperson.
Secondly, what is the problem? Circular references aren't inherantly bad. In
VB6 there was a problem because COM references aren't cleaned up until the
ref count for both objects drop (which never happens in a circular
dependancy unless you resort to some clever tricks), but with a garbage
collector, as long as both aren't reachable, the GC will still clean them
up.

-Rob Teixeira [MVP]
 
A

AlexS

If you can't compile this project with Visual Studio - try SharpDevelop.
However, this is not the point of my remark.
Usually circular references - or perceived as circular - are indicating
design problems. You can consider for example passing frmsearchresult
selection (or which frmperson to new) back to original frmperson or its
parent, which will manage new instance. Maybe person who mentioned events
meant something like this. You can use events or direct properties for this.
And several other techniques. In case of events, you can invoke event
handler when selection changes in frmsearchresult. And handler will be set
by main controller of your application.
Would you care to explain more what is the problem? I might miss something.

HTH
Alex
 
S

Stephen Robertson

Rob,

I agree.

But, let me explain further. frmperson and frmdsearchresult are
individual projects. I have a reference to frmsearchresult in
frmperson. When I try and set a reference to frmperson in
frmserachresult, I get the circular reference error.

The problem is that when I try and set a reference in the
(frmsearchresult) project to the project (frmperson)
 
S

Stephen Robertson

Alex,

Thanks for your reply...

Let me explain further. frmperson and frmdsearchresult are individual
projects. I have a reference to frmsearchresult in frmperson. When I
try and set a reference to frmperson in frmserachresult, I get the
circular reference error.

I whould like to keep the (search) frmperson available is the user
wants to alter the search. This design will allow any number of
frmperson(search) and frmperson(data) and frmsearchresults to be open
at anytime.

I think you are on the right track with the event handlers. If it
will work, I am just not sure how to implement.
 
A

AlexS

Do you need to reference frmperson from frmsearchresult?
If search is done from frmperson, you can show frmsearchresult and get
selection from there. Suppose, you have button on frmsearchresult, which
must show new frmperson. If you want to leave frmsearchresult on screen and
show new frmperson at the same time, you can inform main frmperson that new
one should be created by setting button click event handler to method in
frmperson:

frmperson code:
....
frmsearchresult fs = new frmsearchresult(...);
fs.ShowSelectedPersonButton.Click += new
EventHandler(this.ShowNewPersonUsingSearchResult);
fs.Show();
....

In the event handler you can use some property or method in frmsearchresult
to get person id or whatever you use to init frmPerson.
I see no need to make circular reference in this case.
You might want to check also Observer pattern if you want to implement more
complex scheme of interaction:
http://msdn.microsoft.com/architect...library/en-us/dnpatterns/html/DesObserver.asp

HTH
Alex
 
P

Phill. W

.. . .
We have frmmain calling frmperson (dim f as new frmperson) in
search (no record) mode. When the search is executed, frmperson
calls frmsearchresult (dim f as new frmsearchresult) which is a listing
of persons. From frmsearchresults, frmperson is called (dim f as new
frmperson) with the resulting record.

Would it not be better to pass frmSearchResult a reference to the
existing frmPerson form (object), and allow the results for to reuse this,
rather than creating a new form every time, as in

[frmPerson]
Sub DoSearch( ... ) Handles ...
Dim oSearch as New frmSearchResult
oSearch.DisplayUsing = Me
oSearch.Show[Dialog]()
End Sub

[frmSearchResult]

' ONLY as an example
Public DisplayUsing as frmPerson = Nothing

Sub RowSelected() Handles ...
If ( Me.DisplayUsing is Nothing ) Then
Me.DisplayUsing = New frmPerson
End If
Me.DisplayUsing.Show()
. . .
End Sub

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top