0

I am getting the following error: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

public void LogClientSideErrors(string message, ValidationType validationType)
        => validationType switch
            {
                ValidationType.Success => setInfo(message, controllerInfo),
                ValidationType.Critical => setFatal(message, controllerInfo),
                ValidationType.Error => setError(message, controllerInfo),
                ValidationType.Exception => setError(message, controllerInfo),
                ValidationType.Information => setInfo(message, controllerInfo),
                ValidationType.Warning => setWarn(message, controllerInfo),
                _ => setError("invalid enum value", controllerInfo),
            }; 

Update: Working solution:

internal static void GenerateLogs(string message, string controllerInfo, ValidationType validationType)
        => (validationType switch
        {
            var x when x == ValidationType.Success || x == ValidationType.Information => new Action<string, string>(setInfo),
            ValidationType.Critical => setFatal,
            var x when x == ValidationType.Error || x == ValidationType.Exception => setError,
            ValidationType.Warning => setWarn,
            _ => (_, controllerInfo) => setError("invalid enum value", controllerInfo),
        })(message, controllerInfo);
Iliar Turdushev
  • 4,935
  • 1
  • 10
  • 23
  • Switch expressions are expressions: they're supposed to return a value. You'd probably be better off with a normal switch statement. That way you can group your cases together as well, e.g. `case ValidationType.Success: case ValidationType.Information: setInfo(message, controllerInfo); break;` – canton7 Aug 11 '20 at 13:49
  • This is how I have grouped cases: validationType switch { ValidationType.Critical => setFatal(message, controllerInfo), ValidationType.Exception | ValidationType.Error => setError(message, controllerInfo), ValidationType.Information | ValidationType.Success => setInfo(message, controllerInfo), ValidationType.Warning => setWarn(message, controllerInfo), _ => setError("invalid enum value", controllerInfo), }; – Muhammad Hammad Maroof Aug 11 '20 at 15:24
  • That doesn't do what you think it does. That won't match "Exception OR Error", it does a binary OR of Exception and Error, then sees whether `validationType` is equal to that. Exactly the same as asking `validationType == (ValidationType.Exception | ValidationType.Error)` – canton7 Aug 11 '20 at 15:54

1 Answers1

2

A switch expression expects that you will return something from each branch, but you are attempting to run void methods. You can either make those methods return something, use a regular switch, or you could return Action<string, string> and then call it instead, like this.

public void LogClientSideErrors(string message, ValidationType validationType)
    => (validationType switch
        {
            ValidationType.Success => new Action<string, string>(setInfo),
            ValidationType.Critical => setFatal,
            ValidationType.Error => setError,
            ValidationType.Exception => setError,
            ValidationType.Information => setInfo,
            ValidationType.Warning => setWarn,
            _ => (_, ci) => setError("invalid enum value", ci),
        })(message, controllerInfo);
juharr
  • 31,741
  • 4
  • 58
  • 93
  • Almost but not quite. You need parens around `validationType switch { ... }`, and you need to tell the compiler what the delegate type is. [See here](https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQxAN0QSwGYE8A+AAgEwCMAsAFCEDMABCXQCpyx0DeVd3D9hpANgYAWOgFkAFPwAMdKAEo6AXgB8XHholQ5Ad0wwAxgAt1G7p0pmrdAETYA9vZvKVdAHZwdDUsQA8MlQkAMUd5JFNrbgB9FzoAIQhkCI0AX3kteQBuCIjabyFCURD7KVJZBQ46FNy+QRF4xNLyxXYqqhSgA=) – canton7 Aug 11 '20 at 13:47
  • controllerInfo is a string. Thanks for helping me out. I changed the return type and it worked. I tried the fix you have provided, but it gave the following error: message and controllerinfo doesn't exsit in the current context (rephrased) can you share more implementation details or a reference to follow as I am eager to learn more about it. – Muhammad Hammad Maroof Aug 11 '20 at 14:09
  • @MuhammadHammadMaroof As conton7 mentioned this actually needs parens around the entire switch expression, and it needs one of the options to define the `Action` type. – juharr Aug 11 '20 at 15:14