I recently switched from VB to C#. One thing that I noticed was that in C#, I have problems using comparisons as part of the case. I am not sure how to explain it in words, so here is an example of what I am trying to do.
In VB, my code looks like this and works perfectly fine.
Select Case ExamScore
Case Is >= 90
Grade = "A"
Case Is >= 80
Grade = "B"
Case Is >= 70
Grade = "C"
Case Is >= 60
Grade = "D"
Case Else
Grade = "F"
End Select
In C# on the other hand, Visual Studio tells me that ">=" is an invalid expression.
switch (examScore)
{
case >= 90: grade = "A"; break;
case >= 80: grade = "B"; break;
case >= 70: grade = "C"; break;
case >= 60; grade = "D"; break;
default: grade = "F"; break;
}
Am I doing something wrong here, or is it simply not possible to do this in C#?
Thank you very much in advance!
Top part of this answer is true for C# versions before 7. See below the line for an update for version 7
It's not possible. C# switches can only switch on exact equality:
Each case label specifies a constant value. Control is transferred to the switch section whose case label contains a constant value that matches the value of the switch expression,
You could replace it with a stack of if/else statements, or if you prefer, you can make something that looks quite compact, but some may frown on - a nest of conditional operators:
grade = examScore >= 90 ? "A" :
examScore >= 80 ? "B" :
examScore >= 70 ? "C" :
examScore >= 60 ? "D" :
"F";
With C# 7, switch has been significantly enhanced, and it's now possible to apply more conditions within cases, although it's still not as "clean" as the VB version. E.g. you could do something like:
switch (examScore)
{
case int es when es >= 90: grade = "A"; break;
case int es when es >= 80: grade = "B"; break;
case int es when es >= 70: grade = "C"; break;
case int es when es >= 60; grade = "D"; break;
default: grade = "F"; break;
}
Assuming that examScore is an int, this somewhat abuses the new "pattern matching on types" facility to be able to have something to say in the case clause, and then using the when clauses to apply arbitrary conditions to the newly introduced variable.
Unlike in VB, the C# switch statement is something like "equals" check. So you might need a if else ladder in order to accomplish this.
You may try something like:
private char Grade(int marks)
{
Dictionary<int, char> grades = new Dictionary<int, char> { { 60, 'A' }, { 50, 'B' } };
foreach (var item in grades)
{
if (marks > item.Key)
return item.Value;
}
return 'C';
}
It's not possible in C#.
Use a bunch of ifs instead.
You can have it all in nice function:
public string Grade(int examScore)
{
if(examScore>=90)
{
return "A";
}
if(examScore>=80)
{
return "B";
}
if(examScore>=70)
{
return "C";
}
if(examScore>=60)
{
return "D";
}
return "F";
}
If you really want a switch statement you could use integer division
int i = 69;
switch (Math.Min(9, i / 10))
{
case 9: Grade = "A"; break;
case 6: Grade = "B"; break;
}
Related
I was wondering if I can use a switch with multiple expressions. For example:
string s = "A";
int i = 3;
switch (s, i)
{
case "A", 1:
//DoStuff
break;
case "A", 2:
//DoStuff
break;
case "A", 3:
//DoStuff
break;
...
}
I don't want to use hundreds of if(s == "A" && i == 1)-Statements so it would be great if there's a better solution.
PS: This switch is just an example, I'm actually using it with more complex strings like Names
You certainly can, for example by using tuples:
string s = "A";
int i = 3;
switch (s, i)
{
case ("A", 1):
// DoStuff.
break;
case ("A", 2):
// DoStuff.
break;
case ("A", 3):
// DoStuff.
break;
}
(Basically exactly what you typed, except with the addition of parenthesis in the cases to make them into tuples.)
Note that this requires C# 7 or later.
Since C# 7, it is possible to do the following:
string s = "A";
int i = 3;
switch (s)
{
case "A" when i == 1:
//DoStuff
break;
case "A" when i == 2:
//DoStuff
break;
case "A" when i == 3:
//DoStuff
break;
...
}
Not a problem as much as a curiosity. I came about this old post yesterday and started playing with it in C#. Heres the original post form 2011(How to change vowels in a string to a symbol?).
I changed some of the code along with a counter to count the total letters in the word. I am stuck on the if statement. I know this program may not have any real world purpose, but I'm trying to learn C# string manipulation.
Console.WriteLine("Enter a word.");
string userWord = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("You wrote {0}", userWord);
Console.WriteLine();
userWord.ToLower();
char[] wordArray = userWord.ToArray();
for (int i = 0; i < wordArray.Length; i++)
{
string theLetter = userWord.Substring(i, 1);
theLetter = theLetter.ToLower();
if (wordArray[i] == 'a' || wordArray[i] == 'e' || wordArray[i] == 'i' || wordArray[i] == 'o' || wordArray[i] == 'u')
{
wordArray[i] = '$';
}
string rebuilt = new string(wordArray);
Console.WriteLine("Your word is now: {0}", rebuilt);
Console.WriteLine("The total number of letters in your word is {0}", userWord.Length);
}
Console.ReadLine();
I just want to change the vowels to $ or any other letter or symbol and count the digits in the word.
The first problem with the C program in the post you linked to is that it only changes the lower-case vowels to '$', not the upper-case ones. The second problem is that strings in C# are immutable, and you already worked around this by changing the word into an array, modifying that, and creating a new string from the modified array.
A method to convert all vowels to '$' might look like this:
public static string VowelsToSymbol(string input)
{
if (string.IsNullOrWhiteSpace(input)) return input;
var work = new char[input.Length];
for (int i = 0; i < work.Length; i++)
{
var c = input[i];
switch (c)
{
case 'A': case 'E': case 'I': case 'O': case 'U':
case 'a': case 'e': case 'i': case 'o': case 'u':
work[i] = '$'; break;
default:
work[i] = c; break;
}
}
return new string(work);
}
Slightly less efficient but much better at showing the intent (what is being done) instead of the mechanics (how is it done):
private static char OneVowelToSymbol(char c)
{
switch (c)
{
case 'A': case 'E': case 'I': case 'O': case 'U':
case 'a': case 'e': case 'i': case 'o': case 'u':
return '$';
default:
return c;
}
}
public static string VowelsToSymbolLinq(string input)
{
return string.IsNullOrWhiteSpace(input) ? input :
new string(input.Select(OneVowelToSymbol).ToArray());
}
If you want to allow for accented characters (or whatever else may count for a vowel in some language other than English), things get ugly quite quickly, and you may be better off using a different method (search the internet for '".NET" isvowel' to find examples).
switch(number){
case 2:
a+=b;
break;
case 3:
a+=b;
break;
case 4:
a+=b;
d=f;
break;
case 5:
d=e;
break;
}
how to minimize first three switch cases which which does similar work?
If you using C# 7, you can make use of Pattern Matching, even though this is an overkill as rightly pointed by Jon Skeet. But in case, you want to stick to switch case, and want to reduce 'case', you could do the following
switch(number)
{
case var _ when number == 2 || number==3 || number==4:
a+=b;
if(number ==4)
d=f
break;
case 5:
d=e;
break;
}
You can also replace the first case with variants like
case var _ when new[]{2,3,4}.Contains(number):
Or
case var _ when number >= 2 || number <= 3: // As pointed by earlier answer
Without pattern matching, you could do the following as well
switch(number)
{
case 2:
case 3:
case 4:
a+=b;
if(number ==4)
d=f;
break;
case 5:
d = e;
break;
}
Btw, if your problem is "a+b" is about 60 lines of code, you always have the option to make it a function (and move it out of switch case) to increase its readability.
switch(number)
{
case 2:
case 3:
case 4:
MethodAbAction();
if(number ==4)
MethodDFAction();
break;
case 5:
MethodDEAction();
break;
}
btw, a 60 line method is never fun to read. It would be better if you can split up.
if (2 <= number && number <= 4) {
a += b;
}
if (number == 4) {
d = f;
} else if (number == 5) {
d = e;
}
if (number != 5)
{
a += b;
}
if (number == 4)
{
d = f;
}
else
if (number == 5)
{
d = e;
}
I've made a program where I can decrypt some encrypted text, it does this by reading a text file and shifting the text one letter at a time, and it does this 26 times, but I can't get it to save and write the output to a text file, this is my code below, I would appreciate any help if possible, thank you
using System;
using System.IO;
namespace Assignment1
{
class MainClass
{
public static void Main(string[] args)
{
//Writing to the screen
Console.WriteLine("Welcome to the Ceaser Cipher Shift Program");
Console.WriteLine("Would You Like to Decrypt a File (y for yes/n for no)");
string userValue = Console.ReadLine();
//User's value is set to userValue
// if the user types "y", activate the decryption method
if (userValue.ToLower() == "y")
{
decryption();
}
// if the user types in "n", write "Goodbye" and close the program
if (userValue.ToLower() == "n")
{
Console.WriteLine("Goodbye");
Console.ReadLine();
Environment.Exit(0);
//Environment.exit closes the program
}
}
//Decryption method
public static void decryption()
{
//ShiftLine is equal to new char
char[] ShiftLine = new char[0];
//If the shift is smaller or equal to 25, continue to shift one at a time
for (int shift = 1; shift <= 26; shift++)
{
//This reads the encryptefd text file into the program
string textFile = #"C:\Users\Anthony\Desktop\caesarShiftEncoded.txt";
//The string "test" reads all the lines of the textFile
string[] text = File.ReadAllLines(textFile);
foreach (string line in text)
{
//Sets currentLetter to 0
int CurrentLetter = 0;
int[] ShiftNumbers = new int[line.Length];
ShiftLine = new char[line.Length];
foreach (char letter in line)
{
ShiftNumbers[CurrentLetter] = ConvertLetterToNumber(letter);
ShiftNumbers[CurrentLetter] = ShiftCipher(ShiftNumbers[CurrentLetter], shift);
ShiftLine[CurrentLetter] = ConvertNumberToLetter(ShiftNumbers[CurrentLetter]);
CurrentLetter++;
}
Console.WriteLine(string.Join("", ShiftLine));
}
//Console.WriteLine (textFile.Length);
Console.WriteLine("This is Shift No: {0}", shift);
}
Console.WriteLine("Which Shift Would You Like To Write to a Text File: ");
string userNumber = Console.ReadLine();
if (userNumber.ToLower() == "1 =< 26")
{
using (StreamWriter text = new StreamWriter("TextWrittenFile.txt"))
{
foreach (char line in ShiftLine)
{
text.WriteLine(ShiftLine);
File.WriteAllText("C:\Users\Anthony\Desktop\DecryptedText.txt", ShiftLine);
}
}
}
}
public static int ShiftCipher(int Number, int Shift)
{
if (Number == 27)
{
return 27;
}
else if (Number == 28)
{
return 28;
}
else if (Number > Shift)
{
return (Number - Shift);
}
else if (Number <= Shift)
{
return (26 + (Number - Shift));
}
else
{
return 0;
}
}
public static int ConvertLetterToNumber(char Letter)
{
switch (Char.ToLower(Letter))
{
case 'a':
return 1;
case 'b':
return 2;
case 'c':
return 3;
case 'd':
return 4;
case 'e':
return 5;
case 'f':
return 6;
case 'g':
return 7;
case 'h':
return 8;
case 'i':
return 9;
case 'j':
return 10;
case 'k':
return 11;
case 'l':
return 12;
case 'm':
return 13;
case 'n':
return 14;
case 'o':
return 15;
case 'p':
return 16;
case 'q':
return 17;
case 'r':
return 18;
case 's':
return 19;
case 't':
return 20;
case 'u':
return 21;
case 'v':
return 22;
case 'w':
return 23;
case 'x':
return 24;
case 'y':
return 25;
case 'z':
return 26;
case ' ':
return 27;
default:
return 0;
}
}
public static char ConvertNumberToLetter(int Number)
{
switch (Number)
{
case 1:
return 'a';
case 2:
return 'b';
case 3:
return 'c';
case 4:
return 'd';
case 5:
return 'e';
case 6:
return 'f';
case 7:
return 'g';
case 8:
return 'h';
case 9:
return 'i';
case 10:
return 'j';
case 11:
return 'k';
case 12:
return 'l';
case 13:
return 'm';
case 14:
return 'n';
case 15:
return 'o';
case 16:
return 'p';
case 17:
return 'q';
case 18:
return 'r';
case 19:
return 's';
case 20:
return 't';
case 21:
return 'u';
case 22:
return 'v';
case 23:
return 'w';
case 24:
return 'x';
case 25:
return 'y';
case 26:
return 'z';
case 27:
return ' ';
default:
return '0';
}
}
}
}
As your ShiftLine seems to be an array of chars it could be as simple as that:
File.WriteAllText("C:\Users\Anthony\Desktop\DecryptedText.txt", new string(ShiftLine));
(instead of your loop [btw: Your loop overwrites the file on any turn...])
Actually there are a few things you could improve.
If the user shall only be able to press y or n, you could use the Console.ReadKey() method instead of letting the user input a complete line of text. By using the overloaded method and specifying true as argument, you won't even see the pressed key on written into the console window (Console.ReadKey(true)).
So you could exchange the current if statements with switch (Console.ReadKey(true).Key) and build cases like case ConsoleKey.Y:
So the Main method would look something like that:
public static void Main(string[] args)
{
//Writing to the screen
Console.WriteLine("Welcome to the Ceaser Cipher Shift Program");
Console.WriteLine("Would You Like to Decrypt a File (y for yes/n for no)");
switch (Console.ReadKey(true).Key)
{
// if the user types "y", activate the decryption method
case ConsoleKey.Y:
decryption();
break;
// if the user types in "n", write "Goodbye" and close the program
case ConsoleKey.N:
default:
Console.WriteLine("Goodbye");
Environment.Exit(0);
break;
}
}
I guess the next thing you want to do is using Caesar cipher for encrypting/decrypting data.
Therefore you "just" need to shift every character and this can be done much easier than writing switch-case statements like you did. The idea behind this type of encryption is, that you take one character and replace it with another one which is offset characters away. Decrypting goes the other way.
So it would be much easier building one method that has the ability to do exactly that for a specified text.
There are two special cases you need to consider: What happens if the value you are looking at is on the lower boundary and you substract the offset (meaning, the "new" value would be lower than zero). The second one represents the same situation on the upper boundary.
These sitations can be resolved using either if statements or possibly easier by just looking at the rest of the division with the maximum value range. (text[i] + offset) % NumberOfPossibleCharacters should do the trick.
Knowing this you can easily build a method like the following one (where I have assumed, that there is no character with a value greater than 127):
static string Encrypt(string data, int offset)
{
string result = string.Empty;
for (int i = 0; i < data.Length; i++)
{
// Add the offset to the current character and just take the
// rest of the division by 127. Afterwards cast it back to a character
// (because it will be a number due to + and %)
result += (char)((data[i] + offset) % 127);
}
return result;
}
Be advised, that string operations like += will cause the creation of a new string each time so it is not a good example from an (memory)performance point of view. There is the better way of using the StringBuilder class.
I hope this hint's you in a better way of solving your homework ;-)
private void btnCalculate_Click(object sender, RoutedEventArgs e)
{
double pay;
pay = 0.00;
double add;
add = 0.00;
int age;
age = int.Parse(txtAge.Text);
string month;
month = txtMonth.Text;
if (age >= 18 && age <= 55)
{
pay = 350;
}
else if (age <= 18)
{
pay = 150; //if-else-if statements depending on age
}
else if (age > 55)
{
pay = 35;
}
switch (month)
{
case "January":
case "january":
case "July":
case "july": //switch statement, how much you pay depending on month
add = 100;
break;
case "February":
case "february":
case "August":
case "august":
add = 120;
break;
case "March":
case "march":
case "September":
case "september":
add = 140;
break;
case "April":
case "april":
case "October":
case "october":
add = 160;
break;
case "May":
case "may":
case "November":
case "november":
add = 180;
break;
case "June":
case "june":
case "December":
case "december":
add = 120;
break;
}
lblTotal.Content = (pay + add) * 1.13; //calculation that prints to the label
}
So when I run the code it just outputs 0 in the label
If I put the Calculation at the bottom (seen here) it will say something about the label not being reachable. Any help would be great. Code has been solved
You can directly assign variables. Declaration and assignment need not be different statements.
18 has two different checks: >= 18 in the if, and <= 18 in the first else if. Not a code error but a semantics error.
The final assignment is inside the switch block and unreachable. Put it outside the switch.
It looks like you're using WPF? If so, you should have a look at the MVVM pattern, as well as data binding. It's a good bit to learn and not easy but very important in WPF. It will eliminate the need for querying and writing properties of elements in most cases, though – because that will be handled by the runtime.
Also consider using a ComboBox for the month. Way easier to validate the data.
As suggested your label assignment was inside the switch statement causing it not to execute unless the month was June or December.
In any case, I'd suggest though that you simplify.
Try this:
private void btnCalculate_Click(object sender, RoutedEventArgs e)
{
int age = int.Parse(txtAge.Text);
double pay = age <= 18 ? 150.0 : (age > 55 ? 35.0 : 350.0);
int index = (DateTime.Parse("1 " + txtMonth.Text).Month - 1) % 6;
double[] choices = new [] { 100.0, 120.0, 140.0, 160.0, 180.0, 120.0 };
double add = choices[index];
lblTotal.Content = (pay + add) * 1.13;
}
The label assignment was inside the switch statement. There's a lot of other improvements you can make to code as well.
For starters, you can also join assignment and declaration of variables and use .ToLower() in switch to save yourself the extra cases:
private void btnCalculate_Click(object sender, RoutedEventArgs e)
{
var pay = 0.00;
var add = 0.00;
var age = int.Parse(txtAge.Text);
var month = txtMonth.Text;
if (age >= 18 && age <= 55)
{
pay = 350;
}
else if (age <= 18)
{
pay = 150;
}
else if (age > 55)
{
pay = 35;
}
switch (month.ToLower())
{
case "january":
case "july":
add = 100;
break;
case "february":
case "august":
case "june":
case "december":
add = 120;
break;
case "march":
case "september":
add = 140;
break;
case "april":
case "october":
add = 160;
break;
case "may":
case "november":
add = 180;
break;
}
lblTotal.Text = Convert.ToString((pay + add) * 1.13); //calculation that prints to the label
}