Link type not passed as link

I have this strange problem in my unit tests. See the following code

_pos = null;
Utilities.InitPOS(_pos, trans);
Assert.IsNotNull(_pos); //fails

Functions InitPOSlook like

public static void InitPOS(POSImplementation pos, Transaction newTransaction)
{
    pos = new POSImplementation();
    pos.SomeProp = new SomeProp();
    pos.SomeProp.SetTransaction(newTransaction);
    Assert.IsNotNull(pos);
    Assert.IsNotNull(pos.SomeProp);
}

An object POSImplementationis an implementation of some interface and is a class, so it is a reference type ...

Any idea?

+3
source share
3 answers

You are passing a link to an object InitPOS(namely a link null), not a link to a variable with a name _pos. The effect is that a new instance is POSImplementationassigned to a local variable posin the method InitPOS, but the variable _posremains unchanged.

Change your code to

_pos = Utilities.InitPOS(trans);
Assert.IsNotNull(_pos);

Where

public static POSImplementation InitPOS(Transaction newTransaction)
{
    POSImplementation pos = new POSImplementation();
    // ...
    return pos;
}
+8
source
pos = new POSImplementation();

, , - pos ? ref ?

+1

You do not pass the instance by reference , you pass the link by value .

+1
source

Source: https://habr.com/ru/post/1757990/


All Articles