10 Replies - 1106 Views - Last Post: 29 March 2013 - 03:19 PM Rate Topic: -----

#1 amino1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 13

How to react if two conditions are met?

Posted 29 March 2013 - 02:09 AM

In code below everythig works well, the event is fired when variable counter reaches 0, but the quesion is HOW to fire event if it depended on SOME (NOT one) variables ???:
public partial class Form1 : Form
    {
        Counting count = new Counting();
        public Form1()
        {
            InitializeComponent();
            count.CountFinished += new MyEvent(count_CountFinished);
            button1.Click +=new EventHandler(button1_Click);
        }
        public delegate void MyEvent(object sender,  Info e);
        public class Counting
        {
            public int counter = 8;
            public int anotherVariable = 0;      // QUESTION ?????? - how to implement THIS ???
            public event MyEvent CountFinished;

            protected virtual void OnCountFinished(Info e)
            {
                if (CountFinished != null)
                    CountFinished(this, e);
            }
            public void Decrease()   //I do not like it...To fire the event I MUST manually invoke
            {                        // this method from somewhere outside the class  
                counter--;           // is this the only way ??
                if (counter == 0)
                  
                    OnCountFinished(new Info("Done !"));
            }
        }
        public class Info : EventArgs
        {
            public string info;
            public Info(string inf)
            {
                info = inf;
            }
        }
                //some other place in code...:      
                   count.Decrease();  //  !!FIRING EVENT !! OK

         // but ..IN ANOTHER PLACE OF CODE :
               count.AnotherVariable = value ; //say, by invoking ANOTHER method
               

           
        public void count_CountFinished(object sender, Info e)
        {
           // some code 


The question is : HOW TO FIRE EVENT when BOTH conditions were fulfilled i.e.
create an Instance of Counting - count, then FIRE event, when BOTH (or as many as you can)
count.counter = value1;
count.AnotherValue = value2;
count.AnotherValue2 = value3;
Must I always fire "manually" a method similar to Dicrease, checking "state" of class instance or there's other way ??

Is This A Good Question/Topic? 0
  • +

Replies To: How to react if two conditions are met?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: How to react if two conditions are met?

Posted 29 March 2013 - 05:27 AM

I think you need to review the CLR concept of events some more. In the C#/VB.NET/other managed language view of the world, calling Decrease() on line 39 is not considered manually firing an event. On line 39, all you did is call the method. In running Decrease(), it's logic ends up firing the event on line 20.

Anyway, to answer your question, you would need to implement code similar to the event you have for Counter, but have it key off AnotherValue and AnotherValue2. If you want it to key of value changes, declare AnotherValue and AnotherValue2 as properties rather than raw public variables. Another alternative is to implement the IPropertyChangedNotify interface, but it's still the same mechanics where you'll need to write code to key off the value changes.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5931
  • View blog
  • Posts: 12,853
  • Joined: 16-October 07

Re: How to react if two conditions are met?

Posted 29 March 2013 - 05:30 AM

public class Counting {
	// never have public variables
	private int counter, anotherVariable;
	public event MyEvent CountFinished;
	
	// use a constructor
	public Counting(int counter, int anotherVariable) {
		this.counter = counter;
		this.anotherVariable = anotherVariable;
	}
	
	// public void Decrease()   //I do not like it...To fire the event I MUST manually invoke
	// yeah, I don't like it either
	// expose your variables through properties
	public int Counter {
		get { return counter; }
		set {
			counter = value;
			// check to see if you should fire your event
			StatusChanged();
		}
	}

	public int AnotherVariable {
		get { return anotherVariable; }
		set {
			anotherVariable = value;
			// check to see if you should fire your event
			StatusChanged();
		}
	}
	
	// what's your criteria?
	private bool NeedFire() { return counter==0 && anotherVariable==10; }

	private void StatusChanged() {
		if (CountFinished != null && NeedFire()) {
			CountFinished(this, new Info("Done !"));
		}
	}
}



Hope this helps.

This post has been edited by baavgai: 29 March 2013 - 05:31 AM

Was This Post Helpful? 2
  • +
  • -

#4 amino1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 13

Re: How to react if two conditions are met?

Posted 29 March 2013 - 06:10 AM

To Baavgai
GREAT Answer . Thanks !
That's what I was looking for : EVERY time ANY Variable (Property) is updated, "STATE" (STATUS) of class Instance is checked and in some cases (if TRUE) - Event is Fired.!
Nice Method : NeedFire() ;-)
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5931
  • View blog
  • Posts: 12,853
  • Joined: 16-October 07

Re: How to react if two conditions are met?

Posted 29 March 2013 - 06:43 AM

You might also find INotifyPropertyChanged interesting. It's used extensively in some .NET data binding. Basically, your class sets up a single event which is uses to signal if any property has changed. Handy for all kinds of things.
Was This Post Helpful? 1
  • +
  • -

#6 amino1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 13

Re: How to react if two conditions are met?

Posted 29 March 2013 - 12:19 PM

There's another interesting thing in this example :
If the class Counting was much complicated it'd be useful checking different, dynalically changing conditions.
Wouldn't it be convenient using DELEGATE for our method ?


public delegate NeedSpeed bool(bool need) ;

private bool NeedFire_n() { return variable1==xxx && variable==yyy &&...variable_n=...; }


...and changing StatusChanged() :

...&& NeedSpeed(NeedFire_n)

Though I have an idea, there's a problem :
" The name 'NeedSpeed' does not exist in the current context "
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: How to react if two conditions are met?

Posted 29 March 2013 - 12:29 PM

That's because you named the delegate 'bool'. Try using
public delegate bool NeedSpeed(bool need);


Edit: For some reason I can't spell 'public'

This post has been edited by Momerath: 29 March 2013 - 12:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 amino1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 13

Re: How to react if two conditions are met?

Posted 29 March 2013 - 12:51 PM

that's not the point. HERE I made mistake, sorry.
In real program there is correct :
public delegate bool NeedSpeed(bool need);

and then :
....&& NeedSpeed(NeedFire))
{
....
}
And ERROR: " The name 'NeedSpeed' does not exist in the current context "
Was This Post Helpful? 0
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: How to react if two conditions are met?

Posted 29 March 2013 - 01:34 PM

NeedSpeed is a type (of delegate). You'll need to actually create a variable of that type and assign a method to it.

Check this out.

This post has been edited by Momerath: 29 March 2013 - 01:38 PM

Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5931
  • View blog
  • Posts: 12,853
  • Joined: 16-October 07

Re: How to react if two conditions are met?

Posted 29 March 2013 - 02:17 PM

View Postamino1, on 29 March 2013 - 03:19 PM, said:

Wouldn't it be convenient using DELEGATE for our method ?


Absolutely! But I wasn't going to spring that on you.

However, since you asked:
using System;
using System.Diagnostics;

namespace ConsoleApplication1 {
	public class Foo {
		private int x, y;
		public event EventHandler NotifyStatus;

		public Foo() {
			x = y = -1;
			this.NeedFireCheck = (foo) => { return false; };
		}

		public Predicate<Foo> NeedFireCheck { get; set; }

		public int X {
			get { return x; }
			set { if (x != value) { x = value; StatusChanged(); } }
		}

		public int Y {
			get { return y; }
			set { if (y != value) { y = value; StatusChanged(); } }
		}

		private void StatusChanged() {
			if (NotifyStatus != null && NeedFireCheck(this)) {
				NotifyStatus(this, EventArgs.Empty);
			}
		}

		public override string ToString() { return "(" + X + ", " + Y + ")"; }
	}



	class Program {
		static void LoopTest(Foo foo, int width, int height) {
			for (int y = 0; y < height; y++) {
				foo.Y = y;
				for (int x = 0; x < width; x++) {
					foo.X = x;
				}
			}
			Debug.WriteLine("---");
		}

		static void Main(string[] args) {
			var foo1 = new Foo() { NeedFireCheck = (o) => { return o.X == o.Y; } };
			foo1.NotifyStatus += (o,e) => { Debug.WriteLine("x==y: " + o); };
			LoopTest(foo1, 3, 3);

			var foo2 = new Foo() { NeedFireCheck = (o) => { return o.X == 2; } };
			foo2.NotifyStatus += (o, e) => { Debug.WriteLine("x==2: " + o); };
			LoopTest(foo2, 3, 3);
		}
	}
}



Results:
x==y: (0, 0)
x==y: (1, 1)
x==y: (2, 2)
x==y: (2, 2)
---
x==2: (2, 0)
x==2: (2, 1)
x==2: (2, 1)
x==2: (2, 2)
x==2: (2, 2)
---



Notice that I didn't create a new EventHandler or a new boolean check. Both of those already exist and meet my needs.
Was This Post Helpful? 0
  • +
  • -

#11 amino1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 13

Re: How to react if two conditions are met?

Posted 29 March 2013 - 03:19 PM

Top Level Coding ;-)
Must think it over, but seems to be extremely useful.
thanks a lot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1