C
C#16mo 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
Kouhai16mo ago
What are you trying to do? Return two values or mutate the passed ints?
m1tten
m1ttenOP16mo ago
well i am just try to update the two variables in my main class
Azrael
Azrael16mo ago
Just return X2 and Y2.
Kouhai
Kouhai16mo ago
Well Console.WriteLine(PlayerPositionX + " " + PlayerPositionY); means the variables got mutated, no?
Azrael
Azrael16mo 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
Kouhai16mo 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
m1ttenOP16mo ago
oh i didnt know that
Azrael
Azrael16mo ago
Yes, but he’s not using one.
Kouhai
Kouhai16mo ago
Yes, and so he won't be able to retrun two variables 😅
Azrael
Azrael16mo ago
That’s what I’m concerned about.
Kouhai
Kouhai16mo ago
?? The compiler ignores the second return statement because it would never get executed
Azrael
Azrael16mo 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
Kouhai16mo ago
Ah, I thought you meant it should have a compile time error, my bad!
Azrael
Azrael16mo ago
I should’ve said that from the beginning. My fault.
m1tten
m1ttenOP16mo ago
well thank you this has been very informative i am gonner read up some documation about methods 🙂 have nice day/night/morning
Kouhai
Kouhai16mo ago
Value types - C# reference
Value types vs reference types, kinds of value types, and the built-in value types in C#
Servator
Servator16mo ago
You can't return multiple values like that
Azrael
Azrael16mo ago
We’ve discussed that already.
Tvde1
Tvde116mo ago
I would not recommend using ref for this. I'd recommend making a class Position with an X and a Y
Denis
Denis16mo 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
Tvde116mo 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
Accord16mo 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.
Want results from more Discord servers?
Add your server