I've created some code as seen below for declaring the values of an array. However, I know there is another way to do this using if statements, would you be able to show me that way too? N,Q,L,R and K are the valid codes and the "default" one is when an invalid code (any letter except for the listed ones) is given.
Have put code below - also let me know if more info is needed:
public void setInfo(string c)
{
switch (c)
{
case "K":
event_code = event_codes[0];
break;
case "L":
event_code = event_codes[1];
break;
case "R":
event_code = event_codes[2];
break;
case "Q":
event_code = event_codes[3];
break;
case "N":
event_code = event_codes[4];
break;
default:
event_code = "I";
break;
}
}
CodePudding user response:
I'm not going to give you the code as it is better to understand the process of using switch
or if
statements.
A switch
statement just tells the compiler to compare the value of the parameter provided against each of the case
statement values and if true to run the code between that and the next break
keyword. At the break
further processing of the switch
stops.
Thus the switch
can be thought of as:
switch(s)
---> is parameter s
case "R":
---> equal to "R"
if it is then run
event_code = event_codes[2];
default
---> s
is not equal to any case
condition so run
event_code = "I";
An if
clause must return a logical result i.e., the statement must be true or false.
An if
structure would be:
if (<some logical test>)
{ // test is true
<run this code>
}
else
{ //test is false
<run this code>
}
The switch
can be replaced by a number of if
s, but as you can see you will need one if
for each case
.
Don't forget the default
clause - this is code that is run when all of the test conditions have failed and that you will need to duplicate the behaviour of the switch by not processing more if
clauses after one has passed its logical test and run the code in the test is true
section.
CodePudding user response:
For things like this, I like to use a hashmap, this makes it easier to update your list of values instead of adding more if statements every time you have a new code. Here's a quick example.
static void Main(string[] args)
{
// use the set_event_code function to set the event code
String event_code = "A";
event_code = set_event_code(event_code);
// output the event code
Console.WriteLine("Event Code: " event_code);
}
// create a function that sets the value of "event_code"
public static string set_event_code(String event_code)
{
// create a hashmap of event_codes and the event_code passed will be the key to the value being set
Dictionary<string, string> event_codes = new Dictionary<string, string>();
event_codes.Add("A", "event_code_1");
event_codes.Add("B", "event_code_2");
event_codes.Add("C", "event_code_3");
event_codes.Add("D", "event_code_4");
event_codes.Add("E", "event_code_5");
// set the value of "event_code" to the value of the key passed, if the key does not exist, set the value to "event_code_0"
if (event_codes.ContainsKey(event_code))
{
event_code = event_codes[event_code];
}
else
{
event_code = "event_code_0";
}
return event_code;
}