Troubleshooting this TutorialSometimes, even though you try hard to understand the information in a tutorial, things don't work out quite like you want it to. This page is here to help you resolve any problems you might be having with the tutorial on enumerations. The Common Mistakes section describes common problems that people have when doing the things in this tutorial, and how to resolve them. The Frequently Asked Questions section describes questions that people have that aren't related to mistakes, but rather, trying to understand the stuff better or exploring how it can be used. If your problem or concern isn't addressed here, feel free to add a comment below, so that I know where you're having trouble. I like to keep these pages fairly clean, so I may remove comments that I felt like have been addressed. If I remove your comment and you don't feel like the problem has been fixed, repost the question and we'll take another look at it. If a tutorial has a mistake in it, I will fix the mistake and reply to the comment with a brief explanation. However, after a couple of weeks I'll likely go back and remove the original comment as well as my reply, because, hopefully, the problem will have been fixed, and it won't be a concern any more. |
Common MistakesNone listed yet… |
Frequently Asked QuestionsNone listed yet… |
When doing the final part of this tutorial… specifically…
int dayAsInt = (int)DaysOfWeek.Sunday;
DaysOfWeek today = (DaysOfWeek)dayAsInt; // but this is an implicit cast, so…
DaysOfWeek tomorrow = (dayAsInt + 1); // so you can do this, without explicitly stating the cast
In the last line defining the 'tomorrow' variable C# marks this part as an error…
(dayAsInt + 1)
And gives this error description in the error list window.
Cannot implicitly convert type 'int' to DaysOfWeek'. An explicit conversion exists (are you missing a cast?)
I understand what the example is trying to teach. That you can increment and decrement enumerations like integers.
Which would be valueable in game programming such as the old Heroes of Might and Magic games where the days of
the week would be tracked because various things occured on different days. Such as new troops being hirable ever Sunday.
I tried on my own to get this line working and even went back to the type casting tutorial to see if I
could find anything there to help me out. I managed to get something that outputs the correct
results, in this case printing that tomorrow will be monday. But I still wanted to make sure I notified
you of the issue I encountered and also to ask if how I did it was correct.
I replaced this line…
DaysOfWeek tomorrow = (dayAsInt + 1); // so you can do this, without explicitly stating the cast
With this one…
DaysOfWeek tomorrow = (DaysOfWeek)dayAsInt + 1; // so you can do this, without explicitly stating the cast
Is there anything wrong with the way I did it?
Was I perhaps missing something with the way yours was in the tutorial that was causing it not to work?
(I did have the days of the week properly enumerated outside of the class.)
Thank you in advance for your time and thank you very much for the tutorials. They have been a god send!
For once I feel like I'm actually LEARNING programming and not just typing stuff in without any
understanding of the mechanics of it.
I was going through these tutorials with no major issue until this. Thanks for posting. I can see how you got yours working which is a big help in figuring it out. I have more experience with C++ and have been going through these tutorials so I can start using XNA.
One thing I am curious about is using numbers greater than the Enumeration:
When we enumerated our list the days of the week goes from 0-6. So, if I put in DaysOfWeek tomorrow = (DaysOfWeek)dayAsInt + 7; and have Console.WriteLine(tomorrow); I don't get a day as an output, I get the number 7. I was trying to see if at a week later it would give me Sunday again.
How would I go about calculating the days for a week later, three weeks from now, or last week?
When doing a similar exercise in C++ I use switch statements with a case for a string for the day of the week and an int for the position of the day.
Thanks again,
-L-
I know this post is now completely ancient, but if you're doing a lot of work with dates (figuring out what day of the week some specific date is, etc.) you probably want to use the DateTime struct. One piece that is especially useful is its ability to do formatting. I won't get into all of the details here and now (I don't even know how interested in this you are, at this point) but here is an example.
Post preview:
Close preview