Toggle Boolean Function

Hi there, I’m just trying to create a function where I can specify a variable I want to toggle on and off using a parameter and then toggle that variable. I’m using C#.

I keep getting an error saying I can’t convert a ‘string’ to a ‘bool’.
Here’s what I’ve got so far.

        public bool variableA = true;
        public bool variableB = true;
        public bool variableWithADifferentNameC = true;
        
        public void InvertBoolean (string booleanString)
        	{
        		if (booleanString)
        		{
        			booleanString = false;
        		} else {
        			booleanString = true;
        		}
        	}
        
   

 void RandomFunction ()
    {
InvertBoolean("variableA");
InvertBoolean("variableB");
InvertBoolean("variableWithADifferentNameC");
    }

Any help is much appreciated.

P.S. It doesn’t like lines 7, 9 and 11.

Line 7, if statement requires a boolean. You must explicity verify what you consider a valid string.

booleanString == null

booleanString == “”

booleanString.Length > 0

Line 9,11, strings are not boolean, you could not assign a boolean into a string.

booleanString = “true”;

This is a basic programing concept. I really recommend to find a programing tutorial.

Yeah, you are no where near correct. Im hesitant to correct your error because its not that your code is wrong so much as your mindset and understanding of C# is wrong.

public  class Test
{
    public bool variableA = true;
    public bool variableB = true;
    public bool variableWithADifferentNameC = true;

    public void InvertBoolean(bool arg)
    {
        // warning, value assigned is not used.
        arg = !arg;
        // Try reading the difference between structs and objects
    }


    void RandomFunction()
    {
        InvertBoolean(variableA);
        InvertBoolean(variableB);
        InvertBoolean(variableWithADifferentNameC);
    }
}