❔ Hiya i am new to C# i am having problems with return method
thank you so much in advance
22 Replies
What are you trying to do?
Return two values or mutate the passed ints?
well i am just try to update the two variables in my main class
Just return X2 and Y2.
Well
Console.WriteLine(PlayerPositionX + " " + PlayerPositionY);
means the variables got mutated, no?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.
If you want to mutate the int variables you have to pass them by ref
This would make any modification to X and/or Y in the method body be reflected on the variables you passed
ValueTuple
oh i didnt know that
Yes, but he’s not using one.
Yes, and so he won't be able to retrun two variables 😅
That’s what I’m concerned about.
??
The compiler ignores the second return statement because it would never get executed
His current function is wrong. You can’t return two integers from that function.
Yeah.
That’s what I’m saying?
Unreachable code.
Ah, I thought you meant it should have a compile time error, my bad!
I should’ve said that from the beginning.
My fault.
well thank you this has been very informative i am gonner read up some documation about methods 🙂
have nice day/night/morning
Well, make sure to read this as well
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types
Value types - C# reference
Value types vs reference types, kinds of value types, and the built-in value types in C#
You can't return multiple values like that
We’ve discussed that already.
I would not recommend using
ref
for this. I'd recommend making a class Position
with an X and a YWhy create a clas when there is one already? Isn't
Point
available out of the box?
Anyway, you could return a value tuple:
You use this method like so:
Alternatively
or with the above method:
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.