i have 100 booleans. but i do not want to create 100 different function to change each boolean (to prevent script from going too long).
i like a create just 1 function that can change selected boolean to true using button. end result i hope to have 100 buttons that can change each boolean using that one function.
for the image below on button on click, i was expecting a input field for selecting either of the boolean instead of the checkbox. any way to change that?
public bool bool1 = false;
public bool bool2 = false;
public bool bool3 = false;
....
public bool bool100 = false;
public void ChangeBool(bool a)
{
a = true;
}
CodePudding user response:
I think you're looking for the ref
keyword. As the name implies, this keyword indicates that a reference will be passed to the function rather than just the value (for value types). This means you can set the input parameter using the ref
keyword and the variable passed to the method will be changed as well.
public static void ChangeBool(ref bool a)
{
a = true;
}
Usage:
var b = false;
ChangeBool(ref b);
Console.WriteLine(b) // Prints "True"
Here's a .NET Fiddle with a working example.
However, I fail to see how this is any better than just setting the variable itself to true
.
CodePudding user response:
public bool haveEaten = false;
public bool haveDrank = false;
public bool havePlayed = false;
public void ChangeBool(int a)
{
if(a==1)
{
haveEaten = true;
}//do this for all three booleans and pass an interger from your button. You can also use a switch case.
}