1

It is possible to write one method that can set value to different variables which are loaded by parameter?

I think something about like this:

void SetBooleanValue(bool myVariable, bool newValue)
{
    myVariable = newValue;
}

and then use this like that:

bool isConnected = false;
bool isFinished = true;

public ClassConstructor()
{
    SetBooleanValue(isConnected, true);
    SetBooleanValue(isFinished, false);
}

The problem is my method only gets value of isConnected and isFinished and can't modify original values of those variables.

How to get reference to them?

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
mmm
  • 260
  • 4
  • 21

1 Answers1

2

That´s what the ref-keyword is for:

void SetBooleanValue(ref bool myVariable, bool newValue)
{
    myVariable = newValue;
}

Use it like this:

SetBooleanValue(ref isConnected, true);

This keyword enables you to set the provided instance to something different.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111