C-SHARP - 3.15 Enum (Demo)
We find ourselves now to put into practice the notion of enumeration with a quick and very simple exercise. In this exercise, the program should display It's the weekend, is it such a day if it is Saturday or Sunday And if not well the program will have to show us it is the week, it is Monday or Tuesday Wednesday etc everything will depend on the day that you are going to put in the variable day however if we change the program will have to adapt automatically. And for this exercise, you must absolutely use the enumeration above and for the display, I leave you to search on your side, it is very simple. This exercise is very simple, do not search complicate. Pause the video and we meet just after for the correction. We meet now for the correction, I hope you succeeded. Otherwise, pay attention to the correction. First, we had to first think about how our program should work to deal with whether we are on weekdays or weekends. It was very simple, here we initialized our first constant of our enumeration to 1 so that the days of the week do not start at 0 but at 1. Ok, now we know that Friday is the last day of the week and here it is equivalent to 5 so anything above 5 will correspond to the weekend, i.e. here we know that when the value of a constant is strictly greater at 5 then we will be on the weekend. What had to be done is what? c is an if and between the parentheses we put the day variable which stores a constant of the enumeration > Strictly greater than 5. On the other hand if we do this, will it work? You remember that in the previous video, we saw that to retrieve the value of a constant from the enumeration, what had to be done? We had to add parentheses with the int type in front of the variable to force the conversion to int and here this is what we are going to do to be able to carry out our condition. Then it was necessary to display It's the weekend, are we you had to put either Saturday or Sunday depending on what you were going to put in the day variable. So you had to do a Console.WriteLine() and inside you can choose the display you want, either with the string.Format function or directly with quotes. And to process the weekend, I will use the Format function and for the week I will process the basic display, as it allows you to review all the notions. So, in the Console.WriteLine() I put the Format method belonging to the string class, so I make string.Format brackets its quotes and I put it's the weekend, we are and here I put {0} to be able to recover the day variable that I will put after the comma and so no matter what day we will put we will have a program that adapts. And after the comma I put the day variable. Then you must display for the days of the week, if the if treats all the cases which are greater than 5 then to have the days of the week, it is necessary the opposite or we must do an else which will treat all the other lower cases or equal to 5 because the days of the week values are between 1 and 5. And to finish you must display C is the week, so we are here we are doing a. Console.WriteLine and inside we will use the classic display so we make quotes we write C is the week, we are and outside the quotes, we make a + to concatenate with the day variable. Here with the day variable, we will retrieve the name of the constant that is stored in day. Now, if we launch the program, we have It's the weekend, it's Saturday here Saturday is worth 6, we are well above 5 or the weekend the condition is true, and we execute the Console.WriteLine and here we recover the name of the constant, if we put Sunday and I raise we have the same result except that we do not have Saturday which is displayed but Sunday. And if I now test with a day of the week for example Wednesday, and I run the program, well we enter the else since the value of the constant which corresponds to Wednesday is equal to 3. Here we test with the if , it is not strictly greater than 5 so we enter the else which processes all the other cases, and we execute the instruction which is to display It's the week, it's Wednesday. You see I used 2 ways for the display, both work, after to avoid errors in the display recommend the first It is already easier to visualize the output with the Format function whereas here we concatenate but we are not sure that we obtain the desired result. That's all for this video, I hope in any case that you liked it and that everything was clear for you. See you next time in a new video