Monitoring a boolean?

T

Torben Jensen

Hi,

In my application (c#) I have a boolean that is set to true everytime a
documents content is altered. Everytime the user saves the document my
boolean is set to false.

I want to be able to monitor this boolean. It would be great if I could
create an event that is fired everytime my boolean changes it's value,
something like:

myBool.Changed += new EventHandler(........);

But boolean doesn't have a changed event by default.

How do I create my own event, that monitors the booleans value?

Thank you
Torben
 
C

Claes Bergefall

Wrap it in a property. Then you can monitor it's changes
from inside the set method

/claes
 
W

Wiktor Zychla

How do I create my own event, that monitors the booleans value?
write a class that encapsulates required functionality.

Wiktor Zychla

public class BoolMonitor {
public event EventHandler Changed;

private bool val;
public bool Val
{
get { return val; }
set
{
val = value;
if ( Changed != null ) Changed( this, new EventArgs() );
}
}
}

class MainClass
{
public static void Main()
{
BoolMonitor b = new BoolMonitor();
b.Changed += new EventHandler( bChanged );
b.Val = false;
}

static void bChanged( object sender, EventArgs e )
{
Console.WriteLine( "changed" );
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top