1

I have 4 arrays, each holds different values relating to hours of the day thus:

int[] HourArray1 = {00, 04, 08, 12, 16, 20};
int[] HourArray2 = {01, 05, 09, 13, 17, 21};
int[] HourArray3 = {02, 06, 10, 14, 18, 22};
int[] HourArray4 = {03, 07, 11, 15, 19, 23};

Belo is what I have been able to do to get the index of a particular hour and the array where it is located.

       private void btnGetHexValue_Click(object sender, EventArgs e)
    {
        int HourIndex = 0;
        int HourGroup = 0;

        // Hours
        HourIndex = Array.IndexOf(HourArray1, Convert.ToInt32(DateTime.Now.ToString("HH")));
        HourGroup++;

        if (HourIndex == -1)
        {
            HourIndex = Array.IndexOf(HourArray2, Convert.ToInt32(DateTime.Now.ToString("HH")));
            HourGroup++;

            if (HourIndex == -1)
            {
                HourIndex = Array.IndexOf(HourArray3, Convert.ToInt32(DateTime.Now.ToString("HH")));
                HourGroup++;

                if (HourIndex == -1)
                {
                    HourIndex = Array.IndexOf(HourArray4, Convert.ToInt32(DateTime.Now.ToString("HH")));
                    HourGroup++;
                }
            }
    

My request: Is there a better and more efficient approach please?

Many thanks in advance.

3
  • 1
    Can you please elaborate the question better? Commented Sep 19, 2022 at 17:14
  • 2
    Use "HourIndex % 4" which is the MOD function. The first array is where HourIndex evenly divides by 4. Second array is where the remainder is 1. Third array is where remainder i2. Fourth array is where remainder is 3. Commented Sep 19, 2022 at 17:16
  • 4
    It looks like you are looking for code review of working code - there is Code Review which is more suitable for this type of activity. (Note that DateTime.Now changes every now and then - so make sure to call it once). Commented Sep 19, 2022 at 17:16

1 Answer 1

2

You can put the arrays into another array and use a single LINQ query:

int[][] allHours = { HourArray1, HourArray2, HourArray3, HourArray4 };
(int hourIndex, int hourGroup) = allHours
        .Select((arr, ix) => (Position: ix + 1, FoundIndex: Array.IndexOf(arr, DateTime.Now.Hour)))
        .Where(x => x.FoundIndex >= 0)
        .Select(x => (x.FoundIndex, x.Position))
        .DefaultIfEmpty((-1, -1))
        .First();

Demo: https://dotnetfiddle.net/ACDrz9

Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks for your response. How do I implement your suggestion in C# winform.
@Topa: I don't understand your question. My code is not related to winforms/webforms or any other technology. It just relies on System.Linq and arrays. I think for the exact syntax you need C#7 or newer(tuple deconstruction etc.).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.