简体   繁体   中英

Why does this method not change the global variable?

The global variable is not changed when used as a parameter in a method. It remains the same. I would like it to change accordingly to the methods algorithm.

//Test
float globalBill;

public Form1()
{
  InitializeComponent();
  saveGlobalBill(globalBill);
  Console.WriteLine("Global bill now = " + globalBill); //Console returns 0
}

public void saveGlobalBill(float bill)
{
   bill += 5;
   Console.WriteLine("In method bill is " + bill); //Console returns 5
}

First of all pass your global variable as an argument by reference means just add ref keyword in your method signature like

public void saveGlobalBill(ref float bill)  //<= Notice this "ref" keyword here
{
    //Your stuff here
}

Then call this method from your constructor like

saveGlobalBill(ref globalBill);  //<= Notice this "ref" keyword here

Question: What's the role of ref keyword?

Answer: When used in a method's parameter list, the ref keyword indicates that an argument is passed by reference, not by value. The effect of passing by reference is that any change to the argument in the called method is reflected in the calling method.

That means your bill float parameter is treated as a reference to globalBill .

You can read more about ref keyword from docs.microsoft.com

Since globalBill is a primitive type, it's passed by value, so the variable that you're changing is the variable local to the saveGlobalBill method, not the global variable (global for all intents and purposes in this case, at least, though it's local to the class containing your code).

Since it's a global variable, you don't need to pass it as an argument at all. What you're looking for is the following:

//Test
float globalBill;

public Form1()
{
    InitializeComponent();
    saveGlobalBill();
    Console.WriteLine("Global bill now = " + globalBill); //Console returns 0
}

public void saveGlobalBill()
{
    globalBill += 5;
    Console.WriteLine("In method bill is " + globalBill); //Console returns 5
}

Alternatively, as others say, pass it by reference, but in that case I don't see the point of declaring it as a global value in the first place.

when you pass parameters by value methods do not effect value of themes to changing value of passed parameters you must pass theme as reference parameters.

//Test
float globalBill;
public Form1()
{
  InitializeComponent();
  saveGlobalBill(ref globalBill);
  Console.WriteLine("Global bill now = " + globalBill); //Console returns 0
}

public void saveGlobalBill(ref float bill)
{
   bill += 5;
   Console.WriteLine("In method bill is " + bill); //Console returns 5
}

when you pass float data to parameter, it's copied .

So whatever you do in the function, the value that passed by parameter wouldn't be change when you get out of the function.

Here is the way to do what you want.

Pass the parameter using ref keyword

//Test
float globalBill;

public Form1()
{
  InitializeComponent();
  saveGlobalBill(ref globalBill);
  Console.WriteLine("Global bill now = " + globalBill); //Console returns 0
}

public void saveGlobalBill(ref float bill)
{
   bill += 5;
   Console.WriteLine("In method bill is " + bill); //Console returns 5
}

And I recommend you to study about "call by value" and "call by reference".

I hope it helps.

Thank you. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM