Passing by reference, variables from 3 different methods

user1

New member
Joined
Nov 18, 2014
Messages
3
Programming Experience
Beginner
Hi,

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:

C#:
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,124
Location
Sydney, Australia
Programming Experience
10+
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