Solved

events

Posted on 2006-07-01
17
299 Views
Last Modified: 2008-02-01
Is it possible to raise an event if a varibale changes?

for example:

like :

bool reaiseEvent= false;



public void boolEventHandler(...)
{
    raiseEvent=false;
}
0
Comment
Question by:dkloeck
17 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 17024571
Hi dkloeck,
----------

not sure what you're exactly trying to achieve, from your post i assume you want to disable/enable an event with a boolean value?
i don't think it can be done that way

you can dynamically add or remove an eventhandler as in
http://www.thescarms.com/dotnet/EventHandler.asp

----------
bruintje
0
 
LVL 5

Expert Comment

by:aaronfeng
ID: 17024578
You can expose a event in the class that contain the variable you want others to know it has changed.

      public class Class1 {
            public event EventHandler VariableChanged;

            private bool myVariable = false;

            public bool MyVariable {
                  get { return this.myVariable; }
                  set {
                        this.myVariable = value;
                        if (this.VariableChanged != null) {
                              this.VariableChanged(this, new EventArgs());
                        }
                  }
            }
      }


      public class Class2 {
            private Class1 class1;
            public Class2() {
                  this.class1 = new Class1();
                  this.class1.VariableChanged += new EventHandler(class1_VariableChanged);
            }

            void class1_VariableChanged(object sender, EventArgs e) {
                  // do whatever you want here..
            }
      }

In this case, if MyVariable gets changed it will fire VariableChanged event.  Class2 will be notified by calling the event handler.

Aaron
http://geekswithblogs.net/afeng/
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17024614
I cant access to an instance of a class I need, and I need to raise a method in it.
Yes I know it is dirty programming but i just dont see another way..

So what I want to do is, put a static variable in the class,
and if i change this variable raise the method.

aaronfeng , your method can not be uset to do so.. because i may be changing the variable in another instance.

btw. thatnks for the fast answer you two (I'm still reading your page bruintje)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:dkloeck
ID: 17024682
No.. I don't think it works neither
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 17024776
dkloeck, can you tell us any more about the problem?  I think it is still unclear what your are trying to accomplish.  A variation on aaronfeng's solution follows, but it still produces an effect in all instances of your class.

  using System;

  class Program
  {
    static void Main(string[] args)
    {
      using (new EventClass())
      {
        EventClass.RequestEvent(null, null);
      }

      Console.ReadLine();
    }
  }

  class EventClass : IDisposable
  {
    private static event EventHandler EventRequested;

    public static void RequestEvent(object sender, EventArgs e)
    {
      Console.WriteLine("EventClass is sending event request to all instances.");

      if (EventRequested != null)
        EventRequested(sender, e);
    }

    public static void ReleaseInstances()
    {
        EventRequested = null;
    }

    private EventHandler eventRequestedHandler;

    public EventClass()
    {
      eventRequestedHandler = new EventHandler(OnEventRequested);
      EventRequested += eventRequestedHandler;
    }

    public static void OnEventRequested(object sender, EventArgs e)
    {
      Console.WriteLine("Instance of EventClass is processing event request.");
    }

    public void Dispose()
    {
      EventRequested -= eventRequestedHandler;
    }
  }
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17024788
I make an example..

i have a
public class A{


}

und I need to call a method in it which only works in one instance lets say

public class A{

  public void myMethod()
  {
    MessageBox("RUN!");
  }
}

instance may be:

A instanceOfA;

I'm on a complitly other place of the program (even another project) and I need that instance to execute myMethod()

and i thought that it maybe would be possible if i put a static variable in the class and put an eventHandler on it or something, so that instance may see it and run the myMethod like:

public class A{
 public staic bool runEvent = false;
  public void myMethod()
  {
    MessageBox("RUN!");
  }

  public void runEventVariableChanged(...)
  {
    myMethod();
  }

}
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17024794
I could then call it by creating a new instance and changing the static variable:

A otherInstance;
otherInstance.runEvent=true;
0
 
LVL 5

Expert Comment

by:aaronfeng
ID: 17024877
"I cant access to an instance of a class I need, and I need to raise a method in it.
Yes I know it is dirty programming but i just dont see another way."...

------

This sounds like a design issue, you can actually fix this if you use singlton or factory pattern.  I'm a little confused on what exactly you are trying to do.  Correct me if I'm wrong, you want to notify (fire an event) when a variable has changed in a class, right?

For example:

public class A {
  bool myBool = false;
}

If myBool changes, you want to fire an event.  The problem you are having is, you want only one instance of class A at any time right?

Try this (follow closely, this code looks very similar to before, but different):

      public class Class1 {
            public event EventHandler VariableChanged;
            private static Class1 instance;
            private bool myVariable = false;

            public static Class1 Instance {
                  get {
                        if (instance == null) {
                              instance = new Class1();
                        }
                        return instance;
                  }
            }

            public bool MyVariable {
                  get { return this.myVariable; }
                  set {
                        this.myVariable = value;
                        if (this.VariableChanged != null) {
                              this.VariableChanged(this, new EventArgs());
                        }
                  }
            }
            
      }

      public class Class2 {
            private Class1 class1;
            public Class2() {
                        // call the Class1.Instance to get the same instance of Class1 everytime.
                  this.class1 = Class1.Instance;
                  this.class1.VariableChanged += new EventHandler(class1_VariableChanged);
            }

            void class1_VariableChanged(object sender, EventArgs e) {
                  // do whatever you want here..
            }
      }

Whenever you need Class1, don't use the new keyword to instantiate a new object, just call Class1.Instance.  You will always get the same copy of Class1.  I think this solves your problem.

Aaron
http://geekswithblogs.net/afeng/
0
 
LVL 5

Expert Comment

by:aaronfeng
ID: 17024889
Oh yeah, you should make the Class1's constructor private, so no one can instantiate Class1 by using the new keyword.

public class Class1 {
  private Class1 {
  }
}

Aaron
http://geekswithblogs.net/afeng/
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17025191
no, the problem i'm having is not that i want always only one instance..its that i want to fire an event on a specific instance i cant access to
0
 
LVL 5

Expert Comment

by:aaronfeng
ID: 17025197
Why can't you access a specific instance?  Where is that instance being created?

Cheers,

Aaron
http://geekswithblogs.net/afeng/
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17025201
in another project, and i cant change the design ( i dont have the "rights") but i can create a new instance and do something with staic fields
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17025206
ohh..and there is only 1 instance of that type, and the one i create to change the static field
0
 
LVL 5

Expert Comment

by:aaronfeng
ID: 17025209
ok, if static field will work for then your problem is solved?  or is there another problem with static field?

Cheers,

Aaron
http://geekswithblogs.net/afeng/
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17026527
It don't seem to work for me..

I wrote a test class which will show you how I want it to work:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Class1 myTestClass = new Class1();
            myTestClass.fireEvent = false;
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 classUsedToFire = new Class1();
            classUsedToFire.fireEvent = true; //this line should make all classes fire the event

        }
    }

    public class Class1
    {
        public static bool fireEventNow = false;
        public bool fireEvent = false;

        public void OnFireEventNow()
        {
            if (fireEvent)
            {
                MessageBox("FIRED!");
            }
        }
    }

as you see after initialize the components i create a instance of Class1 which i want to make fire a method without refering directly to it

That what button1 should do.

How must I change class1 to make it work?
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17026529
sorry my mistake.. it should be:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Class1 myTestClass = new Class1();
            myTestClass.fireEvent = true;
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 classUsedToFire = new Class1();
            classUsedToFire.fireEventNow = true; //this line should make all classes fire the event

        }
    }

    public class Class1
    {
        public static bool fireEventNow = false;
        public bool fireEvent = false;

        public void OnFireEventNow()
        {
            if (fireEvent)
            {
                MessageBox("FIRED!");
            }
        }
    }
0
 
LVL 5

Accepted Solution

by:
aaronfeng earned 500 total points
ID: 17026928
ok, I took your example and changed a few lines to make it work.  The code looks odd, but I think it's what you want.

      public partial class Form1 : Form {
            public Form1() {
                  InitializeComponent();
                  Class1 myTestClass = new Class1();
                  Class1.FireEvent += new EventHandler(Class1_FireEventNow);
                  myTestClass.fireEvent = true;

            }

            void Class1_FireEventNow(object sender, EventArgs e) {
                  // do whatever you want here
                        MessageBox.Show("FIRED!");
            }

            private void button1_Click(object sender, EventArgs e) {
                  Class1 classUsedToFire = new Class1();
                  Class1.FireEventNow = true; //this line should make all classes fire the event

            }
      }

      public class Class1 {
            public static event EventHandler FireEvent;
            public bool fireEvent = false;
            private static bool fireEventNow = false;

            public static bool FireEventNow {
                  get { return fireEventNow; }
                  set {
                        fireEventNow = value;
                        if (FireEvent != null) {
                              FireEvent(new Class1(), EventArgs.Empty);
                        }
                  }
            }

      }

Aaron

Cheers,
http://geekswithblogs.net/afeng/
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question