Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:21:25+00:00 2026-06-05T22:21:25+00:00

I have a class that is in an assembly due to being required for

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:21:26+00:00Added an answer on June 5, 2026 at 10:21 pm

    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:

    Public Sub DrawPanel(ByVal panel As Panel, ByVal drawPanels As Integer, ByVal handler As EventHandler)
        For i As Integer = 0 To drawPanels
            Dim childPanel As New Panel
            '...
            AddHandler panel.Click, handler
            panel.Controls.Add(childPanel)
        Next
    End Sub
    

    Then, on your form, you’d need a method that matches the EventHandler signature, such as:

    Private Sub Panel_Click(ByVal sender As Object, ByVal e As EventArgs)
        Form2.Show()
    End Sub
    

    And then you could call the factory like this:

    factory.DrawPanel(panel, 5, New EventHandler(AddressOf Panel_Click))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that requires a specific method to be called before being
I have a class that is mapped in fluent nhibernate but I want one
I have created a class library (assembly) that provides messaging, email and sms. This
I have a window in one assembly that has a TextBlock control that I
I have a class that implements IHttpModule in a separate assembly from a website.
I have a web solution that contains 5 class libraries and one asp.net web
I have F# class library assembly that contains two functions: let add a b
I have a .NET 4.0 class that uses Assembly.LoadFrom to load a .NET 3.5
I have class that represents users. Users are divided into two groups with different
I have class A such that: class A { static int i; A(); f1();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.