Kinetic Code Camp - Bring your skills, or lack thereof.. :dumpster_fire:

Last type: Ternary operators, which are if/then/else statements in a single line which are mainly used to assign values to variables. Basically the same thing as inline-ifs (IIF) functions in other MS products. It’s main value is conciseness. It otherwise behaves and compiles identically to an if-else block.

You do not use ternaries to control program flow directly. I mean, you could so long as the methods used returned a value compatible with the left side of the assignment. However, you shouldn’t unless you like to make enemies and/or hate yourself. It’s almost always less readable than other conditionals.

Ternary syntax: variable = condition ? value if true : value if false;

Here’s an example from a form customization I did a while back. It was needed because sometimes unpopulated dataviews return -1 as the row. Note that I’m declaring the variable AND conditionally setting it in one line.

int rowNum = edv.ParentView.Row < 0 ? 0 : edv.ParentView.Row;

A traditional if-else would have to be written like this. Note that the variable must be declared before setting it in the conditon blocks.

int rowNum;
if (edv.ParentView.Row < 0)
{
    rowNum = 0;
}
else
{
    rowNum = edv.ParentView.Row;
}

You can nest ternaries, but do not do this. Like ever. Just look at the example below See how bad it is? I cried a little just writing it. Ternaries are about simplicity and brevity, and that goes away when the second ? shows up.

int x = yourString == "A" ? 1 : yourString == "B" ? 2 : 3;

Note that there is ternary operator (?), but also the null-coalescing operator (??). The latter is often found is LINQ queries when outer table joins or nullable fields (date fields in Epicor can return null) are involved. However, it’s useful wheneer you run into null results and need to convert them into a default value or fallback to some other value. Works like ISNULL in SQL, in that if the value left of the operator is null, it will fall back to using the value on the right.

//From a LINQ query that looks for jobs not on the newest part revision.
//It returns the job (j) rev if no record is found for latest rev (lr).
where j.RevisionNum != (lr.NewestRev ?? j.RevisionNum)

//From a EpiCombo populated by a UD table. A blank value is a valid selection.
//However the combo box treats an empty value as null, which caused errors.
//Here is the a solution to this situation using a ternary and a null coalesce.
//They are equivalent.
string ternary= combo.Value == null ? "" : combo.Value.ToString();
string nullCoalesce= (combo.Value ?? "").ToString();

Nullable types and LINQ in general is for a later entry.