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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:53:24+00:00 2026-05-23T06:53:24+00:00

I have a model class with an event which I subscribe from other classes.

  • 0

I have a model class with an event which I subscribe from other classes. I want to subscribe and un-subscribe in each class correctly.

  • First I want to guarantee that in MyClass I unsibscribe only once even that code is in few methods.
  • Second there are other classes except MyClass witch use OnMyEvent so I don’t want to unintentionally unsibscribe from the event in the class.

     MyClass(IModel model)
    {
      _model = model;
      _model.OnMyEvent +=EventHandle;
    }
    Close()
    {
     _model.OnMyEvent -=EventHandle;
    } 
    Disconnect()
    {
     //I want to check if OnMyEvent has already unsibscribed
     //Moreover OnMyEvent is used in other classes and
     //I don't want to mess up with it here 
     _model.OnMyEvent -=EventHandle;
    }
    
  • 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-23T06:53:25+00:00Added an answer on May 23, 2026 at 6:53 am

    If you only subscribe once, it doesn’t matter how many times you unsubscribe – unsubscribing when you don’t have a subscription is a no-op. Equally, the entire point of the event API is that you can’t accidentally unsubscribe other subscriptions (either other types, or other instances of the same type).

    As such, the code as shown should be fine, although it might be worth moving the two calls to a single method that handles this. That might be overkill, though.

    Also, if your type is IDisposable, make sure it gets called in that code-path too (presumably by calling Close()).

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

Sidebar

Related Questions

I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
I have a model which describes an event like this: class Event(models.Model): date =
I have a problem where I dispatch an event in a model class which
I have model: [XmlRoot(ElementName = event, IsNullable=true)] public class Event { public int id
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
I have a model class that uses a custom validator as shown in following
I have a model class ModelHome that is a child of Model ie: class
I have models similar to the following: class Band(models.Model): name = models.CharField(unique=True) class Event(models.Model):
Suppose I have an model object Event that contains a Venue . When I

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.