Alan
Alan

Reputation: 326

Is it possible to iterate through enums like an array by its position value?

Is there a way to either iterate through an enum or retrieve its position in the enum list. I have the following example code.

    private static void Main(string[] args)
    {
        DateTime sinceDateTime;
        Counters counter = new Counters();

        // Iterate through time periods
        foreach (TimePeriodsToTest testTimePeriod in Enum.GetValues(typeof(TimePeriodsToTest)))
        {
            // e.g. DateTime lastYear = DateTime.Now.AddDays(-365);
            sinceDateTime = DateTime.Now.AddDays((double)testTimePeriod);
            var fileCount =
                Directory.EnumerateFiles("c:\\Temp\\")
                    .Count(path => File.GetCreationTime(path).Date > sinceDateTime);

            Console.WriteLine("Files since " + -(double)testTimePeriod + " days ago is : " + fileCount);
            // counter.TimePeriodCount[testTimePeriod] = fileCount;
        }
    }

    public enum TimePeriodsToTest
    {
        LastDay = -1,
        LastWeek = -7,
        LastMonth = -28,
        LastYear = -365
    }

    public class Counters
    {
        public int[] TimePeriodCount = new int[4];
    }

    public class Counters2
    {
        public int LastDay;
        public int LastWeek;
        public int LastMonth;
        public int LastYear;
    }

So I want to store the value fileCount into counter.TimePeriodCount[]. If I can get the 'position value' of testTimePeriod, then that would slot quite nicely into the array counter.TimePeriodCount[]. But I haven't been able to find out how to do that yet.

If LastDay, LastWeek, etc were 1, 2, 3, 4 then that wouldn't be a problem, but they aren't and I have a problem!

Alternatively, would there be a way to store fileCount into Counters2.LastDay, Counters2.LastWeek, etc. on subsequent iterations?

Or am I just approaching this the wrong way?

Update The suggestion given by "KuramaYoko" can work by adding a Dictionary to the solution but I found the solution given by Jones6 to be more elegant as it does not require adding a Dictionary. Thanks for your time and effort as I learnt something from both answers :-)

Update2 Now I understand the way AlexD solution should be used, that is also a very good way to solve the problem. Thanks.

Upvotes: 0

Views: 281

Answers (2)

AlexD
AlexD

Reputation: 32576

You can use Enum.GetValues method to get all enum values. I doubt that the order is guaranteed, so you may want to sort the values.

int[] values = Enum.GetValues(typeof(TimePeriodsToTest))
    .Cast<int>()
    .OrderBy(x => x)
    .ToArray();

for (int k = 0; k < values.Length; k++)
{
    sinceDateTime = DateTime.Now.AddDays(values[k]);
    fileCount = ....
    counter.TimePeriodCount[k] = fileCount;
}

BTW, similarly Enum.GetNames will give you the names.

Upvotes: 5

jones6
jones6

Reputation: 784

You should be able to do this

foreach (var testTimePeriod in Enum.GetValues(typeof(TimePeriodsToTest)).Cast<TimePeriodsToTest>().Select((x, i) => new { Period = x, Index = i}))
{
     counter.TimePeriodCount[testTimePeriod.Index] = fileCount;
}

Upvotes: 1

Related Questions