c# Using enum in Switch Case throwing exception - c#

I am working on .NET 6.0 application, I have enum that I am trying to use in switch as to compare with string value but getting exception.
error
private static bool ValidateAlphanumericCase(string text, string fieldName)
{
if (!string.IsNullOrWhiteSpace(fieldName))
{
var rule = GetRule(fieldName).TxtFieldFormat; // string value
switch (rule)
{
case TextFieldFormat.AlphanumericUpperCase.ToString():
break;
case TextFieldFormat.AlphanumericLowerCase.ToString():
break;
}
}
else
{
new EmptyFieldNameException();
}
return false;
}
enum
public enum TextFieldFormat
{
AlphanumericUpperCase = 0,
AlphanumericLowerCase = 1,
}

TextFieldFormat.AlphanumericUpperCase.ToString()
This is a method invocation expression and it is not a valid pattern for swith statement.
You can find all valid patterns here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns
The closest pattern is type pattern or constant pattern, I guess the compiler recognizes AlphanumericUpperCase as a nested class of TextFieldFormat and fails.
In this case you can use nameof operator.
switch (rule)
{
case nameof(TextFieldFormat.AlphanumericUpperCase):
break;
case nameof(TextFieldFormat.AlphanumericLowerCase):
break;
}

Seems you understood switch-case block a little bit different.
A switch block is simply a shortcut (but more performant shortcut) of many if - else if - else blocks.
Of course they are not the same, but their working logic almost the same.
With this very little info, you can easily think about what's wrong in your code.
Bingo, you're right! Case blocks should check the state. (Boolean value..Just interests with either the given statement results true or false..)
After checking the boolean result, Which case's statement match, code continues on that case block.
So, in your situation your code could be like this :
switch (rule)
{
/// Some statements need to put in paranthesis. Also you would need put business codes of cases into curly braces.
/// I write from my mind.
/// So please try paranthesis and/or braces if this code break.
case rule==TextFieldFormat.AlphanumericUpperCase.ToString():
DoSomethingWithFirstCase(); break;
case rule==TextFieldFormat.AlphanumericLowerCase.ToString():
DoSomethingWitSecondCase(); break;
default: DoSomethingWhenNoMatchWithOtherCases();
}
Hope this helps.

You can also use it this way
TextFieldFormat.AlphanumericUpperCase.ToString("g" or "G")

Related

Can I use regex expression in c# with switch case?

Can I write switch case in c# like this?
switch (string)
case [a..z]+
// do something
case [A..Z]+
// do something
....
Yes you can in C# 7 (and nobody noticed I had used the incorrect range character in the character class .. instead of -). Updated now with a slightly more useful example that actually works:
using System.Text.RegularExpressions;
string[] strings = {"ABCDEFGabcdefg", "abcdefg", "ABCDEFG"};
Array.ForEach(strings, s => {
switch (s)
{
case var someVal when new Regex(#"^[a-z]+$").IsMatch(someVal):
Console.WriteLine($"{someVal}: all lower");
break;
case var someVal when new Regex(#"^[A-Z]+$").IsMatch(someVal):
Console.WriteLine($"{someVal}: all upper");
break;
default:
Console.WriteLine($"{s}: not all upper or lower");
break;
}
});
Output:
ABCDEFGabcdefg: not all upper or lower
abcdefg: all lower
ABCDEFG: all upper
A minor refinement on David's excellent answer using _ as a discard.
Just a very simple string value as an example.
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string userName = "Fred";
int something = 0;
switch (true)
{
case bool _ when Regex.IsMatch(userName, #"Joe"):
something = 1;
break;
case bool _ when Regex.IsMatch(userName, #"Fred"):
something = 2;
break;
default:
break;
}
Console.WriteLine(something.ToString());
}
}
Starting with C# 8.0, the answer is now YES!... sort of.
With 8.0, the switch expression was introduced. You can use that to match many different styles of patterns as show below.
string myValueBeingTested = "asdf";
string theResultValue = myValueBeingTested switch
{
var x when
Regex.IsMatch(x, "[a..z]+") => "everything is lowercase",
var y when
Regex.IsMatch(y, "[A..Z]+") => "EVERYTHING IS UPPERCASE",
"Something Specific" => "The Specific One",
_ => "The default, no match scenario"
};
How it works: The switch expression needs a variable that it can feed the results into. The above sample uses theResultValue variable for that. Next you pass into the switch what is referred to as the range expression represented by myValueBeingTested above. The familiar case construct is replaced with a pattern expression followed by the => token and together referred to as an expressionn arm. Anything on the right side of the arm token will end up in the result variable.
Normally the switch expression requires that patterns be a compile-time constant, and you cannot execute arbitrary code inside a switch expression. However we can use a case gaurd pattern by using the when clause to make use of Regex as shown above. Each case needs to be wired up in a distinct arm with distinct throw-away variables, (x and y above).
The 3rd arm shown above is a compile-time constant string to match some specific text if desired. The 4th arm shown above uses the _ pattern, which is a sort of wildcard similar to the old default: keyword. You can also use null instead of, or in addition to _ to handle null cases separately from anything else.
Try it out in my .NET Fiddle sample here.
You should be able to do something like this:
using System.Text.RegularExpressions;
private void fauxSwitch(string caseSwitch)
{
if(Regex.Match(caseSwitch, #"[a..z]+").Success)
{
//do something
return;
}
if(Regex.Match(caseSwitch, #"[A..Z]+").Success)
{
//do something
return;
}
/*default*/
//do something
}
Although, Pattern matching in C#7 is probably the better option.
No. In C# (prior to 7) the switch statement only accepts constant values.
Using more complex expressions as you've suggested is a feature known as 'pattern matching' in the functional programming world.
Pattern matching is included in C#7

Can I use a variable in a switch statement?

I am coding a text based adventure, and am having a problem. I am trying to make a switch statement case that handles every examination action you want, and am getting this for my code so far:
case "examine" + string x:
//this is a method that I made that makes sure that it is an object in the area
bool iseobj = tut.Check(x);
if (iseobj)
x.examine();
else
Console.WriteLine("That isn't an object to examine");
break;
How do I use a variable in my case statement? I want any string starting with "examine" + (x) to trigger the case.
Your scenario would fit into an if-else statement better than a switch statement. In C#, a switch can only evaluate values, not expressions. This means you can't do:
case input.StartsWith("examine"):
However, you can make this work with an if statement! Consider doing the following:
if (input.StartsWith("examine"))
{
//this is a method that I made that makes sure that it is an object in the area
bool iseobj = tut.Check(x);
if (iseobj)
x.examine();
else
Console.WriteLine("That isn't an object to examine");
}
else if (...) // other branches here

Switch Statement with Enum - Or (||) and (&&)

I'm making a project and I have a question. I have 3 enum states like this:
enum GameLevel
{
Level1,
Level2,
None,
}
There's a part in my code where I want to check if there's any level selected or none, so I wanted to do something like:
case !GameLevel.None
or
case GameLevel.Level1 || GameLevel.Level2
Is This Possible?
Various options:
Use switch with a default:
switch (level)
{
case GameLevel.None:
// Whatever
break;
default:
// Do something else
// break
}
Use switch with explicit cases:
// Note: if you add a Level3, you'd need to change this...
switch (level)
{
case GameLevel.Level1:
case GameLevel.Level2:
// Whatever
break;
}
Use an if statment:
if (level != GameLevel.None)
{
...
}
Switch is not used to get a "its not the case"-case. However you can use the default: case, which always executes, when the other cases are "false".
For your !GameLevel.None better use an if case. if(selectedGameLevel != GameLevel.None) { }
I believe the choice of assignment should be based on how many levels you plan on implementing. For less levels (as shown in your example), I would use if and else if as opposed to using switch. Switch only works better if there are more options, as it is then performed differently for faster execution. Jon Skeet's answer will guide you on how to implement the choice you make.

Preventing If within If functions (or cases)

I'm building a code where I have to go trough alot of if/case functions. This on itself is not really a problem only that alot of these if's are the kinda the same.
In my code it has to go trough an if function, and if there is a match it wont matter what match it is it will have to go trough the same next if. But depending on both answers it will have to go to a different part of the code.
So for exmaple:
Switch(A){
case 1:
Switch(B){
case 1: do11thing();
case 2: do12thing();
}
case 2:
Switch(B){
case 1: do21thing();
case 2: do22thing();
}
}
Does anyone know a solution instead of having to place the Switch(B) a lot of times within my code.
In case you have more jumps than executable code (the case that happens quite often when programming state machines), you may store your logic in some nested Dictionary, List and/or array object like the following:
// Maps A and B values to functionality
var funcs = new[] {
new[] { func00, func01, func02, ... },
new[] { func10, func11, func12, ... },
new[] { func20, func21, func22, ... },
...
};
// Actually runs functionality according to your mapping
funcs[A][B](params);
So, in case of A=1 and B=2, the func12 will be called.
Of course, you may use lambdas (params) => {} as funcs in the structure.
you can use one if for the function returning value 2 , and then inside this if use all possibilities as different switch cases.It will be smoother and faster than multiple if loops.
for ex :-
if (2 == function1) {
switch(value of function 2)
{
case value_1:
..//do whatever u want;
break;
case value_2:
..//do whatever u want;
break;
default:
//do some error handling;
break;
}
}
One thing you can do is combine cases. For example, say you have a string value that's your outer switch variable, and an int that's the inner switch variable. Your original code might be:
switch (theString)
{
case "Foo":
switch (theInt)
{
case 1: DoThing1(); break;
case 2: DoThing2(); break;
// many other cases
}
DoFooThing();
break;
case "Bar":
switch (theInt)
{
case 1: DoThing1(); break;
case 2: DoThing2(); break;
// other cases, same as with "Foo"
}
DoBarThing();
break;
}
You can combine the cases and have a conditional:
switch (theString)
{
case "Foo":
case "Bar":
switch (theInt)
{
case 1: DoThing1(); break;
case 2: DoThing2(); break;
// many other cases
}
if (theString == "Foo")
DoFooThing();
else
DoBarThing();
break;
}
The type of logic you're describing, especially if there are very many cases, is probably best implemented using a lookup table. Doing so makes it much easier to follow the logic. It takes a bit more time to set up, but you can easily see in the table that you construct exactly what will happen with any combination of input values.

C# variable scopes and the "switch" statement?

This kind of code would normally work in PHP, but since the scope is much more strict in C#, it's not. I can't figure out a way to write this code without repeating myself.
static double Cube()
{
Console.Write("Enter the side length of the cube: ");
try
{
double x = Convert.ToDouble(Console.Read());
return Math.Pow(x, 3);
}
catch (FormatException)
{
Console.WriteLine("Invalid input, please enter a number.");
Cube();
}
return 1;
}
..Later in Main():
switch (choice)
{
case 0:
return;
case 1:
double final = Cube();
break;
default:
Console.WriteLine("Please enter 0 or 1.");
Main();
break;
}
Console.WriteLine("The volume is: {0}", Convert.ToString(final));
The Cube() method works fine, but it's messy in my opinion (return 1 at the end to make the compiler happy). But an error comes up saying The name 'final' does not exist in the current context. It can't find final. So the only way to make this work that I'm seeing is to put the Console.WriteLine statement right after the double final = Cube().
I've also tried declaring double final; outside the switch, then just setting final inside each case, but that hasn't worked either.
Thanks!
You're right: this is a mess. Start over.
Your fundamental problem is that you're not separating your concerns. You have a method that does user input, input validation, retry logic and math all at the same time. You should rather make methods for each.
Also, use TryParse to handle the failure case, not exception handling.
Finally, recursion is completely the wrong tool to use. A problem must have the following characteristics to be solved by recursion:
A trivial base case.
Can be reduced to a set of smaller problems.
Solutions to smaller problems can be combined to solve larger problems.
Making a problem smaller repeatedly eventually gets to the trivial case.
Your problem has none of these properties, so recursion is automatically the wrong tool. The tool you want is a loop.
static void Main()
{
double x = ObtainDoubleFromUser(
"Enter the side length of the cube: ",
"Please enter a number: ");
Console.WriteLine("The volume is {0}", Cube(x));
}
static double ObtainDoubleFromUser(string firstMessage, string failureMessage)
{
Console.Write(firstMessage);
while(true)
{
double result;
if (Double.TryParse(Console.Read(), out result))
return result;
Console.Write(failureMessage);
}
}
static double Cube(double x)
{
return Math.Pow(x, 3);
}
Does that all make sense? You want to avoid recursion and exception handling if you possibly can. And keep your concerns separated.
If you want to access final from outside the switch scope, you'll have to declare it outside that scope too. If you reference final and there are code paths that allow not setting a value to final, then the compiler will be "angry".
In php, final would magically be 0 when you don't assign anything to it. Try declaring final before the switch, and then assign a value to it at each case statement including the default case.
Place the variable declaration before your switch statement:
double final = 0.0;
switch(choice)
{
...
}
Then just use the variable in your switch statement:
case 1:
final = Cube();
break;
In C#, variables must be declared before they can be used. In your code, the declaration was limited to the scope of the switch statement. Declaring the variable prior to the switch statement ensures that its in the scope of the method, allowing it to be used inside and after the switch statement.

Categories