Setting up Enum for days of the week

Hi there! So I’ve never really used enums before, and I’m trying to set up an enum for the days of the week and I want to change them when my time script reaches midnight (at hour = 0), but I’m having trouble iterating through the enums.

Here is my enum set up:

private enum days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, Count}

    private days dayOfTheWeek;

So the idea is when I hit midnight, which I’m keeping track of elsewhere in my script, then I want to go to the next day on the enum, and if I’m on the last day (Sunday) I want to wrap around back to the first day.

I did some google searching on this and I couldn’t make any of the solutions I found work so I’d love some more help on this.

Thanks so much!

Just curious, but why are you creating your own enum instead of just using the built in features of Datetime? You can already pass a date to it and get a day of the week back from it.

Otherwise, you also have “count” in your enum, which seems out of place.
Enums are already assigned a value of 0, 1, 2, etc. In your case, 0-6. So you just have to do a check at midnight to see what enum value you are on, and if you’re on 6, go back to the first day of the week. Otherwise, increment by 1.

Two additions to this:

  1. You can explicitly specify number values for each enum value if you don’t want to rely on the compiler assigning its own numbers (I don’t know whether or not it’s considered reliable that the same enum will compile to a given number):
public enum Days {Monday=0, Sunday=1...
  1. You can do the incrementing and looping behavior with the modulus operator:
int newDay = ( (int)oldDay + 1) % 7;

I think DateTime is just for getting the real world values isn’t it?

I’m trying to create my own calendar values for a fantasy RPG so I didn’t think it would work for that.

I didn’t try setting my own values for the days, that might make it a lot easier, thanks for this!

If it uses a calendar with the same rules (and if you have the same days of the week my guess would be you are) then it will work fine. By “same rules” I just mean, the same months, 365 days in a year, 24 hours in a day, etc.

Otherwise, my recommendation would be to learn how DateTime works and use that implementation as your model for your own FantasyDateTime class; DateTime has proven the robustness of its model. Long story short is basically that it uses a single “long” type number to represent the time, and maths out all the other values from that number on demand. You could do this with “number of seconds” as your number, and if you want to add a day to the time, you add 246060 to that number.

Note, DayOfWeek is already an enum in the System namespace:

It’s what’s returned by DateTime.DayOfWeek:

And lastly, DateTime may be used to get the current with the likes of DateTime.Now and DateTime.Today.

But you can instantiate your own DateTimes to represent ANY time you’d like. Combiend with all its helper methods/types, it’s a very powerful tool.

That’s interesting, I didn’t know I’d be able to do that, I’ll look into DateTime to learn more about it, I really have just been ignoring it since I thought it just returned real world time based on your PC settings.

Thanks!