Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# ?: Operator with ref parameter

I have the following class:

public class SubStredisko
{
    public string Name { get; set; }
    public string Code { get; set; }

    public VyplatnePasky VyplatnaPaska { get; set; }
    public MzdoveNaklady MzdoveNaklady { get; set; }
    public Poistne Poistne { get; set; }
}

then I have two SubStredisko items, where one of them is definitely null. What I am trying to do, is that I have a separate method, which does some processing with SubStredisko, such as changes values of VyplatnaPaska etc. Here is what it looks like:

    private static void VyplatnePasky_Items(ref Stredisko stredisko, XElement myElement)
    {
        //some logic here
    }

What I try to do now (what I have problem with) is to call this method using the ?: operator in the following way:

VyplatnePasky_Items((sPracovisko == null) ? ref sPracovisko_Dohodari : ref sPracovisko,xElement);

However it highlights sPracovisko_Dohodari and sPracovisko with the following error: Syntax error, ':' expected.

I tried to put them in brackets separately, however with no luck. What am I doing wrong?

P.S. sPracovisko and sPracovisko_Dohodari are of type SubStredisko.

P.S.2: Just a quick thought - maybe I don't even need a ref parameter in here? I am not quite sure if in this case a new object will be created, or I will be (in my void) directly changing values of that specific object.

like image 928
Robert J. Avatar asked Sep 08 '25 02:09

Robert J.


1 Answers

You can't use an expression for a ref parameter. You need two calls:

if (sPracovisko == null) {
  VyplatnePasky_Items(ref sPracovisko_Dohodari, xElement);
} else {
  VyplatnePasky_Items(ref sPracovisko, xElement);
}
like image 193
Guffa Avatar answered Sep 09 '25 15:09

Guffa