simple bool toggle question... kind of

Ok so currently I have a button that toggles a window on and off. Then I set the window on and off using code and passing a bool.

My question is there any way to combine these two function (below) into one function to save myself a headache?

C#

	public void ProcedureWindowSetup(bool bValue)
	{
		goProcedureWindow.SetActiveRecursively(bValue);
	}
	
	public void ProcedureWindowSetupToggle()
	{
		if(goProcedureWindow.active == true)
		{
			goProcedureWindow.SetActiveRecursively(false);
		}
		else if (goProcedureWindow.active == false)
		{
			goProcedureWindow.SetActiveRecursively(true);
		}
	}

It would be amazingly awesome if I could pass “true”, “false”, and “null” to a function where if I pass true or false it sets the window accordingly and if I pass null it toggles the window.

The anwser is probably that you cant do all of this in one function but … Any ideas?

How about using an enum? E.g. (not compiled or tested):

public enum Action { Activate, Deactivate, Toggle };

public void ProcedureWindowAction(Action action)
{
    if (action == Action.Toggle) {
        goProcedureWindow.SetActiveRecursively(!goProcedureWindow.active); 
    } else {
        goProcedureWindow.SetActiveRecursively(action == Action.Activate); 
    }
}

Disclaimer, didn’t read the rest of your post very carefully so this mgiht not be a good idea from a design point of view, but you can use a nullable bool.

    void test(bool? parameter)
    {
        if (parameter == null)
            Debug.Log("parameter is null");
        else
            Debug.Log("value = " + parameter.Value);
    }

Calling

    test(true);
    test(false);
    test(null);

Will result in

Yeah, using a nullable type here is probably a better solution than the enum, so I’d go with that (unless you have more actions that you want to support).

Yea the nullable bool is great for what I was trying to do however the enum gave me a few good ideas for consolidating some of my code a bit into a few managers.

Thanks for all the great ideas, being a self taught coder is hard because I often overlook simple solutions and my code turns into a 500lb gorilla. :slight_smile:

Thanks again!