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

  • Home
  • SEARCH
  • 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 675841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:51:46+00:00 2026-05-14T00:51:46+00:00

I haven’t found similiar post so I’m asking this. Let’s say I defined somewhere

  • 0

I haven’t found similiar post so I’m asking this.
Let’s say I defined somewhere an application wide available static Property (I mean it’s not local) and in one class I would like to know when this property is being changed. Apart from aop (transparentproxy etc.) which I think doesn’t suit me well here (and I can’t add that to project anyway), what are the options here?

One solution I can think of, which is probably a very nasty one, is to use some event that would be executed in the setter and just attach it in the class(es) which needs that.
Something like:

   public static event EventHandler CurrentNumberChanged= delegate {};
   public static int CurrentNumber
    {
        get
        {
            return currentNumber;
        }
        set
        {
            currentNumber = value;

            CurrentNumberChanged(null, EventArgs.Empty);
        }
    }

I know it’s really unsafe to use such events ( read here ). And since I would use it in asp.net makes it even more ugly. Do you have any advices ?

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

    You could use a variation on the Observer pattern to the same effect. Not sure what your threading requirements are, and I suspect this suffers from similar dereferencing problems as How to raise custom event from a Static Class (although would have to play with the code a bit more to bottom that out):

    using System;
    using System.Collections.Generic;

    namespace ClassLibrary1
    {
    public class StaticObservable
    {
    private static int currentNumber;

        private static readonly List<IObserver> observers = new List<IObserver>();
    
        public static int CurrentNumber
        {
            get{return currentNumber;}
            set
            {
                currentNumber = value;
                foreach (var observer in observers)
                {
                    observer.NotifyChange();
                }
            }
        }
    
        public static void Attach(IObserver observer)
        {
            observers.Add(observer);
        }
    
        public static void Detach(IObserver observer)
        {
            observers.Remove(observer);
        }
    }
    
    public interface IObserver
    {
        void NotifyChange();
    }
    
    public class ObserverImpl : IObserver
    {
        public void NotifyChange()
        {
            Console.Out.WriteLine("Number has changed");
        }
    }
    
    
    public class AppWrapper
    {
        public static void Main (string[] args)
        {
            Console.ReadLine();
    
            var observerImpl1 = new ObserverImpl();
            var observerImpl2 = new ObserverImpl();
    
            StaticObservable.Attach(observerImpl1);
            StaticObservable.Attach(observerImpl2);
    
            StaticObservable.CurrentNumber = 1;
    
            Console.ReadLine();
        }
    }
    

    }

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

Sidebar

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.