Wednesday, January 12, 2011

The art of toggling

Toggline is the idea is to set something that is originally to true to false and vice versa. Or, precisely speaking, we want to negate the logical value.

Say, you originally declared a variable: (let's walk this through in C#)

private bool someValue= false;

In some codes, we often see ...

public void toggleSomeValue(bool b)
this.someValue = b;
However, the above is not exactly toggling. It's more like setting someValue to some arbitrary value that you demand.

It should be re-written as:

public void toggleSomeValue()
this.someValue = !this.someValue;

public void toggle(bool someValue)
// if someValue is declared as class variables
someValue = !someValue;


public bool toggle(bool someValue)
// you actually don't need this, but FYI.
return !someValue;

Then, down the page, you would have a function that makes use of the toggled value

public void doSomethingBasedOnSomeValue()
//do this
//do that

toggleSomeValue(); //or toggle(this.someValue);

-my tuppence worth

No comments:

Post a Comment