shar shar - 3 months ago 11
C# Question

c# passing Parameters values from function to other function

are there any way to get the values from Parameters in "functionone" and calculate it in the "functiontwo" without writing that again that's a small code for example what i mean

public void functionone(int x, int y)
{

x = 1;
y = 2;

}

public void functiontwo(int a , int b )
{
a=x+y;
b=x-y;

Console.WriteLine(a);
Console.WriteLine(b);


}

Answer

You are implementing functionone wrongly I guess doing this: public void functionone(int x, int y) { x = 1; y = 2; } is normally not the way to pass parameters and change its values in the method, or saying in another way, x and y should be holding the values you pass as parameters, and no getting assigned inside the method..

define a global x and global y, then you can access to it everywhere in that scope..

Example:

class Abc{
    int globalX;
    int globalY;
....
public void functionone(int x, int y)
{
   globalX = 1 + x;
   globalY = 2 + y;
}

public void functiontwo(int a , int b )
{
   a=globalX + globalY;
   b=globalX - globalY;

   Console.WriteLine(a);
   Console.WriteLine(b);
}

}