0

So I need to create a class with a number of auto-implemented properties with public getters and no setters which I know how to do but source of one of the properties comes from another public class where I had to create a enum list with [Flags]. How do I reference this property from another class?

class DayoftheWeek
    {
        [Flags]
        public enum DayOfWeek
        {
            Mon = 0,
            Tue = 1,
            Wed = 2,
            Thu = 4,
            Fri = 8,
            Sat = 16,
            Sun = 32
        };
    }
class PreciseDate
    {
        public int dayMonth { get; }       
        
        //public dayWeek => DayOfWeek { get; }  ??? error
    }
Alan Shore
  • 93
  • 7
  • 1
    Are you just looking for `public DayOfWeek dayWeek { get; }`? – Sweeper Jun 28 '20 at 00:54
  • If you are going to have a `[Flags]` enum that represents days of the week, you should name it something other than `DayOfWeek`. A reader will not understand that you can OR those things together. Consider some like `ApplicableDaysOfTheWeek` – Flydog57 Jun 28 '20 at 03:50
  • I'm amused by the fact that the framework already has a class that represents dates more precisely than your PreciseDate – Caius Jard Jun 28 '20 at 03:58

1 Answers1

0

If you have an enumeration that's is declared inside another class rather than being declared inside a namespace then you qualify the property type using classname.enumname. That it is a flags enum is irrelevant, as too is the fact that it is an enum. Anything declared inside another class is qualified with the outer class name followed by the inner class name

Taking in the advice in the comments for better naming too, consider something like:

public class EventSchedules{
  [Flags]
  public enum WeekDays { //name flags enums as plural
    Mon=1,Tue=2,Wed=4,Thu=8,Fri=16,Sat=32,Sun=64
  }
}

public class RecurringEvent{

  public EventSchedules.WeekDays OccursOn { get; private set; }
}

You should consider not to use 0 as a value for your one of your enum items unless you're going to remember to compare it numerically. The normal way to test for a flags enum containing a flag is like one of these:

WeekDays x = WeekDays.Tue | WeeksDays.Wed;

 //this way
if(x.HasFlag(WeekDays.Tue)) ...

//or this way
if(x & WeekDays.Tue == WeekDays.Tue) ...

If you made Mon = 0 then both of these would return true when testing against Monday (x.HasFlag(Mon)) //returns true, isn't true) even if the x didn't contain Monday. This is because you cannot look at a number 7 and say concretely whether it contains a 0 or not - was it 4+2+1 or 4+2+1+0 ?

You could remember to do a straight compare without any masking like if(x == Mon) but then you have to wonder "did it mean that Mon was set, or that the user forgot to set a value?"

Maybe better to just make Mon=1 (and have a None=0)

ps; Public Things In C# Have PascalCase Names

Caius Jard
  • 47,616
  • 4
  • 34
  • 62
  • I am supposed to have a list, from which the output could be "Monday+Tuesday", not just one day. I did this with days because it was easier to explain. Perhaps I should've used my real example. Thank you for you suggestions. – Alan Shore Jun 28 '20 at 15:07
  • It's easy enough, if youre passed an enum that is eg Mon+Tue+Thu, to have a loop on the enum values, and you can ToString them all to become a list. Look at https://stackoverflow.com/questions/4171140/how-to-iterate-over-values-of-an-enum-having-flags. It would be something like: `var e = WeekDays.Mon | WeekDays.Tue; Enum.GetValues(e.GetType()).Cast().Where(e.HasFlag).Select(v=>v.ToString("g")).ToList()` and that would be a list of string "Mon" and Tue" – Caius Jard Jun 28 '20 at 16:09
  • I've created another question with the entire solution in it to address this. It's here: https://stackoverflow.com/questions/62626608/enum-is-defined-but-not-found-in-the-class – Alan Shore Jun 28 '20 at 18:28