I have a class that is in an assembly due to being required for other projects that I’m currently working on.
One class lets call it Class Factory, creates a group of controls which require click event handlers to be attached, I have algorithms which determine polymorphic behavior based on certain features but that is relatively irrelevant.
Due to the event handler having to open up a specific form that isn’t part of my assembly, and that form requiring this “Factory” class. Without creating circular reference is there any way I can essentially “delegate” the handler event for the form to define?
As a simple work around I have had to maintain two separate classes, one in the project with the form and one in the assembly. This is obviously the wrong way to go.
Not 100% sure on the title as I know what i’m trying to do but not sure about how to go about it.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Width = 1500
Dim panel As New Panel
panel.Location = New Point(0, 0)
panel.Size = New Size(1000, 200)
panel.BackColor = Color.Yellow
Me.Controls.Add(panel)
Dim factory As New DrawFactory
factory.DrawPanel(panel, 5)
End Sub
The above is in a separate project to the following code which is in a class library:
Public Class DrawFactory
Private xOffset As Integer = 0
Private Const widthConst As Integer = 50
Public Sub DrawPanel(ByRef panel As Panel, ByVal drawPanels As Integer)
Application.DoEvents()
For i As Integer = 0 To drawPanels
Dim childPanel As New Panel
childPanel.Location = New Point(xOffset, 0)
childPanel.Size = New Size(widthConst, panel.Height)
If i < 1 Then
childPanel.BackColor = Color.Blue
Else
childPanel.BackColor = Color.Pink
End If
xOffset = xOffset + widthConst
''want to add a handler here to open say form5 which is unknown and undefined in the scope of this class
panel.Controls.Add(childPanel)
Next
End Sub
End class
This isn’t my actual code but it portrays the jist of the problem that I’m faced with, I don’t mind an answer in C# or VB.NET.
Circular references are not technically a problem in .NET. It was a problem in COM because COM uses reference counting to determine when objects need to be destroyed. If you had two objects that each referred to each other, the circular reference would cause neither objects’ reference count to reach zero and therefore they would never be destroyed (causing a memory leak). In .NET however, the CLR uses garbage collection instead of reference counting. The garbage collector periodically searches through all your objects to find any that are no longer referenced by your application. It then destroys all those dead objects that if finds. The garbage collector is smart enough to know that two or more objects that are only referenced by each other, and nothing else, are both dead. Therefore, in .NET managed code, circular references no longer cause memory leaks and, therefore, they no longer need to be avoided.
However, that being said, yes, you can pass an event handler method as a delegate to your DrawFactory class in another project. The DrawFactory class could then add that event handler to any events it wants. For instance:
Then, on your form, you’d need a method that matches the EventHandler signature, such as:
And then you could call the factory like this: