Method Call using Ternary Operator
Asked Answered
R

8

52

While playing around with new concepts, I came across the Ternary Operator and its beauty. After playing with it for a while, I decided to test its limits.

However, my fun was ended quickly when I couldn't get a certain line of code to compile.

int a = 5;
int b = 10;
a == b ? doThis() : doThat() 

    private void doThis()
    {
        MessageBox.Show("Did this");
    }
    private void doThat()
    {
        MessageBox.Show("Did that");
    }

This line gives me two errors:

Error   1   Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Error   2   Type of conditional expression cannot be determined because there is no implicit conversion between 'void' and 'void'

I have never used a Ternary Operator to decide which method to be called, nor do I know if it is even possible. I just like the idea of a one-line If Else Statement for method calling.

I have done a bit of research and I cannot find any examples of anyone doing this, so I think I might be hoping for something impossible.

If this is possible, please enlighten me in my wrong doings, and it isn't possible, is there another way?

Ramberg answered 30/3, 2011 at 17:50 Comment(2)
Make sure the functions doThis and doThat return a value of type int.Jefe
If you really want a one-line If Else, just write it as one line: if (condition) doThis(); else doThat(); Granted, the ternary operator (if it worked the way you thought) would be shorter, but terseness isn't always a good thing.Spoilsman
R
3

The reason why the above statement does not work was provided by the other users and effectively did not answer my true question.

After playing around some more, I figured out that you CAN use this operator to do the above statement, but it results in some bad code.

If I were to change the above statement to;

int a = 5;
int b = 10;
int result = a == b ? doThis() : doThat(); 

private int doThis()
{
    MessageBox.Show("Did this");
    return 0;
}
private int doThat()
{
    MessageBox.Show("Did that");
    return 1;
}

This code will compile and execute the way it should. However, if these methods were not originally intended to return anything, and referenced other areas in the code, you now have to handle a return object each time to call these methods.

Otherwise, you now can use a ternary operator for a one-line method chooser and even know which method it called in the next line using the result.

int result = a == b ? doThis() : doThat();

if (result == 0)
   MessageBox.Show("You called doThis()!");

Now, this code is absolutely pointless and could be easily done by a If Else, but I just wanted to know if it could be done, and what you had to do to get it to work.

Now that I know that you can effectively return any type in these methods, this might become a little more useful. It may be considered a "Bad Coding Practice" but might become very useful in situations it was never MEANT for.

You could get access to one object or another based on any condition and that might be very useful in one line of code.

UserPrivileges result = user.Group == Group.Admin ? GiveAdminPrivileges() : GiveUserPrivileges();

private UserPrivileges GiveAdminPrivileges()
{
      //Enter code here
      return var;
}
private UserPrivileges GiveUserPrivileges()
{
      //Enter code here
      return var;
}

Sure, this can be done by an If Statement, but I think that using the Ternary Operator for other uses makes programming fun. Now, this may not be as efficient as an If Else statement, in which case, I would never use this.

Ramberg answered 30/3, 2011 at 18:8 Comment(2)
Agreed, but for completeness, you could also return a bool, string, or any object really. But really, what does the statement 1; really mean to a compiler? Not much, and it doesn't help the nuances of learning good coding practicesBitty
@Bitty I did not know that, and that is the point of my question. I wanted to know how to get this to work and the benefits/consequences of using it. The ability to choose a return type makes it even more useful.Ramberg
G
50

The ternary operator is used to return values and those values must be assigned. Assuming that the methods doThis() and doThat() return values, a simple assignment will fix your problem.

If you want to do what you are trying, it is possible, but the solution isn't pretty.

int a = 5;
int b = 10;
(a == b ? (Action)doThis : doThat)();

This returns an Action delegate which is then invoked by the parenthesis. This is not a typical way to achieve this.

Gmt answered 30/3, 2011 at 17:54 Comment(3)
Indeed, not the correct way to do things; but definitely the most correct answer to the question: the OP wanted to use the ternary operator to choose a method to call, and that's exactly what your snippet does ;)Lehr
Why is this not "correct"? Another poster went so far as to call it dumb.Dominicdominica
this is great with the new expression body operator (=>)Boloney
A
19

Ternary operator must return something. A typical usage is like this:

int x = (a > b) ? a : b;

If you try something like

a + b;

The compiler will complain.

(a > b) ? a - b : b - a;

is basically a shortcut for either "a - b" or "b - a", which are not legitimate statements on their own.

Annalee answered 30/3, 2011 at 17:53 Comment(2)
+1. If DoThis() and DoThat() returned something, you could use the ternary to control execution and assign the result to some local variable, however this is not the intent of the ternary operator; other developers would think the return value was more significant to you than the side effects. Use a basic if statement; your intentions will remain clear (and you don't have to mess with your method signatures just to be clever).Reeding
+1, Another key point is that both values that could be returned from the ternary operator must be of the same type or implicitly convertible to the same type.Drayton
A
11

If you really want to invoke void methods in a conditional operator, you can use delegates:

(something ? new Action(DoThis) : DoThat)();

If the methods take parameters, this will become more complicated.
You can either put lambda expressions in the conditional or use Action<T>.

However, this is a very dumb thing to do.

Amazonite answered 30/3, 2011 at 18:13 Comment(1)
I'm late, I know, but without any further explanation I don't consider characterising anything as 'dumb' appropriate (especially if it somehow looks like working around a feature lacking in the language)...Pride
V
7

You should be able to do this, though:

        int a = 5;
        int b = 10;

        var func = a == b ? (Action)doThis : (Action)doThat; // decide which method

        func(); // call it

Not that it's really that useful though.

Varick answered 30/3, 2011 at 18:3 Comment(1)
It makes more sense to me to have (Action) before each function, but others' example had it just before doThis. Are both correct?Topsoil
R
3

The reason why the above statement does not work was provided by the other users and effectively did not answer my true question.

After playing around some more, I figured out that you CAN use this operator to do the above statement, but it results in some bad code.

If I were to change the above statement to;

int a = 5;
int b = 10;
int result = a == b ? doThis() : doThat(); 

private int doThis()
{
    MessageBox.Show("Did this");
    return 0;
}
private int doThat()
{
    MessageBox.Show("Did that");
    return 1;
}

This code will compile and execute the way it should. However, if these methods were not originally intended to return anything, and referenced other areas in the code, you now have to handle a return object each time to call these methods.

Otherwise, you now can use a ternary operator for a one-line method chooser and even know which method it called in the next line using the result.

int result = a == b ? doThis() : doThat();

if (result == 0)
   MessageBox.Show("You called doThis()!");

Now, this code is absolutely pointless and could be easily done by a If Else, but I just wanted to know if it could be done, and what you had to do to get it to work.

Now that I know that you can effectively return any type in these methods, this might become a little more useful. It may be considered a "Bad Coding Practice" but might become very useful in situations it was never MEANT for.

You could get access to one object or another based on any condition and that might be very useful in one line of code.

UserPrivileges result = user.Group == Group.Admin ? GiveAdminPrivileges() : GiveUserPrivileges();

private UserPrivileges GiveAdminPrivileges()
{
      //Enter code here
      return var;
}
private UserPrivileges GiveUserPrivileges()
{
      //Enter code here
      return var;
}

Sure, this can be done by an If Statement, but I think that using the Ternary Operator for other uses makes programming fun. Now, this may not be as efficient as an If Else statement, in which case, I would never use this.

Ramberg answered 30/3, 2011 at 18:8 Comment(2)
Agreed, but for completeness, you could also return a bool, string, or any object really. But really, what does the statement 1; really mean to a compiler? Not much, and it doesn't help the nuances of learning good coding practicesBitty
@Bitty I did not know that, and that is the point of my question. I wanted to know how to get this to work and the benefits/consequences of using it. The ability to choose a return type makes it even more useful.Ramberg
A
2

The conditional operator is an expression that returns a value.
You cannot use it with functions that return void.

Instead, you should use a normal if.

Amazonite answered 30/3, 2011 at 17:54 Comment(1)
However, maybe the OP was assuming that users could use the conditional operator in a C/C++ style syntax, where the syntax would allow the user to call a method within a conditional operation. But I assume that from what the OP said, they didn't base their use of the conditional operator off of the C/C++ syntax.Cautious
W
2

Method 1: Using Action

Action Action = true is true ? new Action(() => A()) : new Action(() => B());
Action.Invoke();

Method 2: Using Extensions

Button Button = new Button(){
    IsEnabled = false
};
Button.Switch((x) => x.IsEnabled).Invoke(() => A(), () => B())

Extension Switch:

public static R Switch<T, R>(this T sender, Func<T, R> method){
    return method.Invoke(sender);
}

Extension Invoke:

public static void Invoke(this bool condition, Action @true, Action @false){
    Action Action = condition ? @true : @false;
    Action.Invoke();
}

Method 3: Using Extensions and C# 6.0 null conditional operator

Button.Case((x) => 0 > 1)?.With(() => A());
Button.Case((x) => 0 < 1)?.With(() => B());

Extension With:

public static T With<T>(this T sender, Action method){
    method.Invoke();
    return sender;
}

Extension Case:

public static object Case<T>(this T sender, Func<T, bool> method){
    return method.Invoke(sender) ? Convert.ChangeType(sender, typeof(T)) : null;
}
Windburn answered 11/10, 2020 at 14:44 Comment(0)
K
1

.NET doesn't (easily) support (a readable version of) this for a reason. It's very jankity and makes your code hard to read. Logic trees should be relatively easy to traverse. If I were to walk in to a job and all the code they had used ternary for assigning values, calling methods, etc. I think I would just walk out.

Kerek answered 30/3, 2011 at 17:57 Comment(4)
You still need an assignment.Gmt
I would think that the only way this would make code hard to read is if you used non descriptive variable names and magic numbers (numbers that seem to have no meaning). If a ternary operator is used correctly, it should read as easy as an If/Else.Ramberg
Method names can be long and frequently involve parameters. I don't want to have to read a 200 characters line to figure out where one method call starts and the other ends. Multiple lines (at least for me) is generally easier to read and almost never requires multi-line lines. Compare to: (VicePresidentsAttendee.AnnualSalary == MoneyHelper.MonetizeInt(SalaryThreshold)) ? RaiseHelper.AddABillionDollarBonus(VicePresidentAttendee.PersonId) : RaiseHelper.RegisterPersonForYearlyRaise(SalaryThreshold, PersonId, SickDaysLastQuarter, TypingSpeedInGoalWordsPerMinute, LunarPhaseOnLastPiDay);Kerek
jankity? You have killed Google Translator :-) (and Bing also)Shandashandee

© 2022 - 2024 — McMap. All rights reserved.