Reputation: 131
I am new here but have more or less stalked through the forums from time to time.
I am working in c# and this is a homework assignment but I am attempting to use a switch statement to convert 7 ref character variables from a letter to a corresponding number. The question I have is how (if its even possible) can I use multiple variables for one switch statement. The cases will always be the same and I would rather not have 27 cases per switch statement for 7 statements. I would honestly prefer to do a set of if statements but that is out of the question.
Any assistance would be greatly appreciated. Thank you.
Here is my c# code for this particular portion:
static int ToDigit(ref char ch1, ref char ch2, ref char ch3, ref char ch4, ref char ch5, ref char ch6, ref char ch7)
{
ProcessInput(ref ch1, ref ch2, ref ch3, ref ch4, ref ch5, ref ch6, ref ch7);
ch1 = char.ToUpper(ch1);
ch2 = char.ToUpper(ch2);
ch3 = char.ToUpper(ch3);
ch4 = char.ToUpper(ch4);
ch5 = char.ToUpper(ch5);
ch6 = char.ToUpper(ch6);
ch7 = char.ToUpper(ch7);
switch (ch1)
{
case 'A':
Console.Write(2);
break;
case 'B':
Console.Write(2);
break;
case 'C':
Console.Write(2);
break;
case 'D':
Console.Write(3);
break;
case 'E':
Console.Write(3);
break;
case 'F':
Console.Write(3);
break;
case 'G':
Console.Write(3);
break;
case 'H':
Console.Write(3);
break;
case 'I':
Console.Write(3);
break;
case 'J':
Console.Write(3);
break;
case 'K':
Console.Write(3);
break;
case 'L':
Console.Write(3);
break;
case 'M':
Console.Write(3);
break;
case 'N':
Console.Write(3);
break;
case 'O':
Console.Write(3);
break;
case 'P':
Console.Write(3);
break;
case 'Q':
Console.Write(3);
break;
case 'R':
Console.Write(3);
break;
case 'S':
Console.Write(3);
break;
case 'T':
Console.Write(3);
break;
case 'U':
Console.Write(3);
break;
case 'V':
Console.Write(3);
break;
case 'W':
Console.Write(3);
break;
case 'X':
Console.Write(3);
break;
case 'Y':
Console.Write(3);
break;
case 'Z':
Console.Write(3);
break;
default:
Console.Write(-1);
return -1;
Upvotes: 3
Views: 2363
Reputation: 13551
OK, this is a homework assignment, so it is an absolute requirement that you use a switch instead of a dictionary. That is fine, you need to learn both the syntax and the concept, but...
I think there is something else that should be addressed, something a bit more basic than syntax -- and that is the principle of least work. A program of any size is broken up into pieces, methods, subs, functions, objects, routines, modules, etc.
Each piece should do as LITTLE work as is neccessary in order to do what that piece needs to do. Your ProcessInput function takes seven parameters, and then does the exact same (*three) things to all seven parameters.
In order to do that efficiently/maintainably, with all of the code still in that one function, you have to add an additional step, iteration over the seven parameters. Which means that in an effort to make things better you have increased the number of things your function does. You have increased the work that this piece does, but you should always be striving to reduce the work that is done by a given piece. "Perfection is not achieved when there is no more to add, but when there is nothing left to take away". How do you do this? By adding a piece that does less and then delegating the work to that piece.
In short, if you take your existing ProcessInput function that takes seven parameters, and create a ProcessInput function that takes a SINGLE parameter, you will end up with more PIECES, but each of those pieces will do less work. Which means the piece will be easier to understand, easier to design, easier to debug and easier to modify if requirements change.
At that point, you don't even have to have a loop to make the process easier to understand. You could just do call the new function seven times in a row.
*one of the things your current method does, is change the contents of a variable in the method that calls it. This is because you are using ref parameters. I would question whether you actually need to do that. It is seldom required, and frequently misunderstood and misused. For instance, at the time of this writing, ALL of the existing answers have missed it, and if it is in fact an actual requirement, none of them will work.
I hope you find this answer helpful, even though it contains no code.
Upvotes: 2
Reputation: 101681
You can't specify two variable for one switch statement but if you are allowed to use a Dictionary
, and LINQ
, you can simplify your code like this:
var dictionary = new Dictionary<char, int>
{
{'A', 2},
{'B', 2},
{'C', 2},
{'D', 3},
{'E', 3},
{'F', 3},
{'G', 3},
{'H', 3},
{'I', 3},
{'J', 3},
{'K', 3},
{'L', 3},
{'M', 3},
{'N', 3},
{'O', 3},
{'P', 3},
{'Q', 3},
{'R', 3},
{'S', 3},
{'T', 3},
{'U', 3},
{'V', 3},
{'W', 3},
{'Y', 3},
{'Z', 3},
};
var output = string.Concat(new[] {ch1, ch2, ch3, ch4, ch5, ch6, ch7}
.Select(x => dictionary.ContainsKey(x) ? dictionary[x] : char.MaxValue)
.Where(x => x != char.MaxValue));
Console.WriteLine(output);
Upvotes: 0
Reputation: 4079
If you are required to use a switch statement, then Gabriel GM's answer above is the best. However, there are better ways of doing it; the usual way is to have a dictionary object and do a simple key lookup:
private const int NotFound = -1;
private static readonly IDictionary<char, int> MyLookup = new Dictionary<char, int>
{
{ 'A', 2 },
{ 'B', 2 },
{ 'C', 2 },
{ 'D', 3 },
{ 'E', 3 },
{ 'F', 3 },
// and so on
};
public int DoStuff(char ch1)
{
if (MyLookup.ContainsKey(ch1))
{
Console.WriteLine(MyLookup[ch1]);
}
else
{
Console.WriteLine(NotFound);
}
}
Apart from being a lot cleaner to look at than a horribly long switch statement, however well grouped, it has the beauty of allowing you to load the dictionary lookup from some datasource (e.g. webservice, XML file, database) if you wanted to make it configurable - try doing that with a switch statement! Maintenance in the long run would also be more simple - just add another entry to the dictionary, rather than adding another case statement.
Upvotes: 0
Reputation: 473
You could put all the ch* variables into a list (or array) and iterate through each using a for loop.
List<char> chars = new List<char>() { ch1, ch2, ch3, ... };
foreach (var ch in chars)
{
switch (ch)
// Your code from here ...
}
Upvotes: -1