-5

I have a line of code from someone else: something like...

public static bool ScreenToMapPosition (Vector2 ScreenCoord, out Int2 MapPosition)

Just to be clear, am I understanding this correctly?

When I call the function, it returns:

  1. Returns the bool, true/false

  2. ALSO returns the MapPosition?

This is the meaning of "out"?

So in effect, I enter a Int2 MapPosition just so I can have it given a new value?

Almost the same as

public static Int2 ScreenToMapPosition (Vector2 ScreenCoord)
{
    Int2 MapPosition = //blah blah code code;
    return MapPosition;
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user2948630
  • 184
  • 1
  • 8
  • 1
    See http://pobox.com/~skeet/csharp/parameters.html – Jon Skeet Jan 21 '14 at 13:33
  • 2
    Please see this link it is already answered on stack overflow... http://stackoverflow.com/questions/1169786/when-should-i-use-out-parameters – Nil23 Jan 21 '14 at 13:34
  • 1
    @jwg Thank you. All I was asking for was double checking if I understood correctly. You are one of the only people who actually answered the question with a "Yes, that is correct." or "No, this is what it means." – user2948630 Jan 21 '14 at 13:38

4 Answers4

1

So in effect, I enter a Int2 MapPosition just so I can have it given a new value?

I will rephrase this to:

So in effect, I enter a Int2 MapPosition and it will for sure be a new value?

A new instance of an Int2 class will be created and the variable you pass in for the MapPosition parameter will be set to this instance.

out just says that the previous variable value will not be expected and will even be discarded and a new one will be created.

This is different from ref, where you need to pass in an initialized object.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
1

It means the value it's set to in the method, will be the value of the variable you sent when you called it. For example:

Int2 mapPos;
ScreenToMapPosition(screenCord, out mapPos);

Here mapPos will have the value it was set to inside the method ScreenToMapPosition.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

This article on MSDN explains that the out parameter is another way to return values from the method.

This sample of MSDN makes it clear:

static void Method(out int i)
{
    i = 44;
}

static void Main()
{
    int value;
    Method(out value);
    // value is now 44
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
-3

You are correct in that the method returns a boolean value (true/false).

The out keyword on the parameter means that the argument is passed in by reference, meaning a variable declared outside the method can be changed inside the method without being explicitly returned.

bitsi
  • 18
  • 1
  • 2
  • 3
    Wrong. The `ref` keyword does that. The `out` keyword indicates that in any case a new instance will be created within the method and the value you pass in will be discarded. – Thorsten Dittmar Jan 21 '14 at 13:36
  • 2
    By the way: Pass by reference is the default in C# for object parameters. `out` indicates the reference *will* change as a result of calling the method, `ref` means the reference *may* change. – Thorsten Dittmar Jan 21 '14 at 13:48
  • My thank you to bitsi was deleted, obviously by someone who didn't want to know how bitsi's answer was the best or someone who didn't want bitsi to be shown my appreciation for his correct answer. – user2948630 Jan 27 '14 at 16:31