Passing by reference, variables from 3 different methods


New member
Nov 18, 2014
Programming Experience

is it possible to pass by reference using variables from different methods please ? Or is there another way to do this? I want to do this so as i can take all the values from the different methods and write to a file.

an example:

public void minimum()
    int min = 20;
    WriteFile(ref min);

public void maximum()
    int max = 60;
    WriteFile(ref max);

public void average()
   int avg = 40;
   WriteFile(ref avg);

public void WriteFile(ref int min, ref int max, ref int avg)

Thank you for your time.
That makes no sense. Firstly, why would you be passing those parameters by reference? Are you using their current values and then changing them in the WriteFile method and expecting those changes to be reflected in the original methods? That is the only reason to pass by reference and your code snippet does not indicate that you're doing that so there's no reason to pass by reference.

Secondly, if a method has three parameters then you have to pass three arguments. What exactly are you expecting that WriteFile method top do when it assumes that it will receive three values and it only gets one?

Maybe what you should be doing is declaring member variables for those three values and removing all the parameters from the WriteFile method. Then you can set the fields in the first three methods and read them in WriteFile.
Top Bottom