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 6842271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:04:45+00:00 2026-05-27T00:04:45+00:00

public Class A { public A() { someotherclass.someevent += new EventHandler(HandleEvent); } private void

  • 0
public Class A
{

   public A()
   {
     someotherclass.someevent += new EventHandler(HandleEvent);
   }

   private void HandleEvent(object sender,CustomEventArgs e) 
   {
      if(e.Name == "Type1")
           Method1();
      else if(e.Name == "Type2")
           Method2();
   }

   protected virtual void Method1(){}
   protected virtual void Method2(){}
}

public class B: A
{
   public B()
   { /*Something*/}

   protected override void Method1(){/*some logic*/}
   protected override void Method2(){/*some other logic*/}
}

public class C: A
{
   public C()
   { /*Something*/}

   protected override void Method1(){/*some logic*/}
   protected override void Method2(){/*some other logic*/}
}

public class Main
{
    private A;

    public Main(){/*Something*/}

    private void StartB()
    {
       A = new B();
    } 

    private void StartC()
    {
       A = new C();
    } 
}

Now, what happens is, after I go through a cycle in which both the methods StartB(called first) and StartC(called second) are called, when the someevent is triggered, the code tries to execute the Method in Class B(and later Class C, I hope. I could not get there since it errors out when it calls method in Class B), instead which I want it to call only the method in Class C.

I think that, since the event is subscribed at constructor, Class B methods are still getting fired since it is subscribed initially on the call of StartB.

Question:
I want only the methods of the class that is instantiated the latest should be executed.

For Example: if StartB and StartC are called in order, when someevent is triggered the Methods in Class C should only get executed. Same Vice-Versa. How to do that?

I know am doing something terribly wrong. Any help is much appreciated.

  • 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-05-27T00:04:46+00:00Added an answer on May 27, 2026 at 12:04 am

    You aren’t unsubscribing from the event from your first instance so it will be called. If you don’t want it to be called you need to unsubscribe. You could do something like this

    class A
    {
    
    private static EventHandler lastHandler;
    
    public A()
    {
        //warning, not thread safe
        if(lastHandler != null)
        {
            someotherclass.someevent -= lastHandler;
        }
        lastHandler = new EventHandler(HandleEvent);
        someotherclass.someevent += lastHandler;
    }
    

    but it seems pretty hacky. You are probably better off implementing a method (e.g. IDisposable) to clean up your last instance before a creating a new one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Sample{ private const =constant string; public static object StaticMethod(args){ var result= SomeOtherClass.StaticMethod(const,args);
public class InterruptedInput { public static void main(String[] args) { InputThread th=new InputThread(); //worker
public class PackageTabActivity extends ListActivity{ HashMap<String,Object> hm ; ArrayList<HashMap<String,Object>> applistwithicon ; private static final
public class AllViewModel { private List<Settings> SettingsList; public ViewAgendaAllViewModel() { client.SupplierListWithSettings(GetSupplierListWithSettings_Completed) } public void
I have a class with two constructors that look like this: public MyClass(SomeOtherClass source)
public class MyClass { public int Age; public int ID; } public void MyMethod()
public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class CovariantTest { public A getObj() { return new A(); } public static
public class WrapperTest { static { print(10); } static void print(int x) { System.out.println(x);

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.