C
C#2y ago
m1tten

❔ Hiya i am new to C# i am having problems with return method

static void Main(string[] args)
{
Console.WriteLine("Hello World!");
int PlayerPositionX = 5;
int PlayerPositionY = 5;
Add(PlayerPositionY, PlayerPositionX);
Console.WriteLine(PlayerPositionX + " " + PlayerPositionY);
//it should be both 10 10 but instead only printing 5 5
}
public static int Add(int X, int Y)
{
X = X + X;
Y = Y + Y;

return X;
return Y;

}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
int PlayerPositionX = 5;
int PlayerPositionY = 5;
Add(PlayerPositionY, PlayerPositionX);
Console.WriteLine(PlayerPositionX + " " + PlayerPositionY);
//it should be both 10 10 but instead only printing 5 5
}
public static int Add(int X, int Y)
{
X = X + X;
Y = Y + Y;

return X;
return Y;

}
thank you so much in advance
22 Replies
Kouhai
Kouhai2y ago
What are you trying to do? Return two values or mutate the passed ints?
m1tten
m1ttenOP2y ago
well i am just try to update the two variables in my main class
Azrael
Azrael2y ago
Just return X2 and Y2.
Kouhai
Kouhai2y ago
Well Console.WriteLine(PlayerPositionX + " " + PlayerPositionY); means the variables got mutated, no?
Azrael
Azrael2y ago
Then you’d have to define them outside of main. private int X = 5 I’m on my phone, excuse my typos. How do you return two variables in an integer function? I’m so confused.
Kouhai
Kouhai2y ago
If you want to mutate the int variables you have to pass them by ref
public static int Add(ref int X, ref int Y)
public static int Add(ref int X, ref int Y)
This would make any modification to X and/or Y in the method body be reflected on the variables you passed
Add(ref PlayerPositionX, ref PlayerPositionY)
Add(ref PlayerPositionX, ref PlayerPositionY)
ValueTuple
m1tten
m1ttenOP2y ago
oh i didnt know that
Azrael
Azrael2y ago
Yes, but he’s not using one.
Kouhai
Kouhai2y ago
Yes, and so he won't be able to retrun two variables 😅
Azrael
Azrael2y ago
That’s what I’m concerned about.
Kouhai
Kouhai2y ago
?? The compiler ignores the second return statement because it would never get executed
Azrael
Azrael2y ago
His current function is wrong. You can’t return two integers from that function. Yeah. That’s what I’m saying? Unreachable code.
Kouhai
Kouhai2y ago
Ah, I thought you meant it should have a compile time error, my bad!
Azrael
Azrael2y ago
I should’ve said that from the beginning. My fault.
m1tten
m1ttenOP2y ago
well thank you this has been very informative i am gonner read up some documation about methods 🙂 have nice day/night/morning
Kouhai
Kouhai2y ago
Value types - C# reference
Value types vs reference types, kinds of value types, and the built-in value types in C#
Servator
Servator2y ago
You can't return multiple values like that
Azrael
Azrael2y ago
We’ve discussed that already.
Tvde1
Tvde12y ago
I would not recommend using ref for this. I'd recommend making a class Position with an X and a Y
Denis
Denis2y ago
Why create a clas when there is one already? Isn't Point available out of the box? Anyway, you could return a value tuple:
public (int x, int y) Add(int x, int y)
{
return (x+x, y+y);
}
public (int x, int y) Add(int x, int y)
{
return (x+x, y+y);
}
You use this method like so:
var (x, y) = Add(5, 10);

Console.WriteLine($"My X value is now {x}");
var (x, y) = Add(5, 10);

Console.WriteLine($"My X value is now {x}");
Alternatively
var result = Add(5, 10);

Console.WriteLine($"My X value is now {result.x}");
var result = Add(5, 10);

Console.WriteLine($"My X value is now {result.x}");
Tvde1
Tvde12y ago
or with the above method:
var x = 1;
var y = 2;

(x, y) = Add(x, y);

Console.WriteLine($"Updated X and Y are: {x}, {y}.");
var x = 1;
var y = 2;

(x, y) = Add(x, y);

Console.WriteLine($"Updated X and Y are: {x}, {y}.");
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?