lte__ lte__ - 1 month ago 8
C# Question

C# - Value assignment not working?

I'm completely puzzled by the following. I have an input file which has some characters that I'd like to read into a

String array
. The code looks like this:

public void SetType(Int32 x, Int32 y, String value)
{
if (x < 0 || x >= _fieldTypes.GetLength(0))
throw new ArgumentOutOfRangeException("x", "The X coordinate is out of range.");
if (y < 0 || y >= _fieldTypes.GetLength(1))
throw new ArgumentOutOfRangeException("y", "The Y coordinate is out of range.");
if (!CheckStep(x, y))
return;

_fieldTypes[x, y] = value;
} //breakpoint in debug


The function gets called so that x and y range from 0 to 8, so that
0,0
,
0,1
,
0,2
gets called and so on.

I'm checking this with the debugger.
value
is equal to
"f"
,
_fieldTypes
is a
{string[8, 8]}
, yet even after
_fieldTypes[x, y] = value;
the whole
_fieldTypes
array consists of
null
values only (for the rest of the values, too):
enter image description here
How is this possible? The function is being called, I'm checking with the debugger, the breakpoint is the last line.

Thank you for your help!

Answer

If CheckStep returns true then the function returns. Although the breakpoint on the closing bracket is hit, because the function will return its result (void in this case). Then the assignment to the array has not been made and all values are null.

Please see the example below. The function returns and the last breakpoint his hit after the first one.

You can verify this by setting a breakpoint on the line

_fieldTypes[x, y] = value;

I bet, that this line will not be hit - or the value of value is null.

Comments