Solved

Find position in array?

Posted on 2008-06-17
7
1,099 Views
Last Modified: 2012-05-05
I have a question... Not really sure this is the way to do it, so i am open to any other suggestions..

I want to split a 24 hour day up into 2 hour periods... I want to then look at the current hour and get the period this hour fits into.

So i built an array:

int[] HourPeriods = new int[12] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 };

And can get the current hour, and loop through the hour periods...

        foreach (int hr in HourPeriods)
        {
            //DateTime.Now.Hour;
        }

But how to i do this? Say if the hour is 15 how do i get the 16 period from the array???

Nugs
0
Comment
Question by:Nugs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21807813
I think you're making it harder than it has to be:
int PeriodIndex = DateTime.Now.Hour / 2;
0
 
LVL 15

Assisted Solution

by:Solar_Flare
Solar_Flare earned 100 total points
ID: 21807821
well you can test the DateTime.Now.Hour against the int

 foreach (int hr in HourPeriods)
        {
            if (DateTime.Now.Hour < hr && DateTime.Now.Hour > (int-2))
            {
                  //datetime.now.hour is less than 2 hours before int hr
            }
        }
0
 
LVL 2

Author Comment

by:Nugs
ID: 21808058
jcoehoorn: I think you are right that i am making it more complex than it need be. Unfortunatley your example is way off from what i am looking for.

Solar_Flare: Just by looking at your example i could see it would work. But after much consideration and jcoehoorn comment i decided it was to complicated for what needed to be done...

What do you guys think of the attached code...

Since it is the hours i am looking at these number will always be between 1 and 24... Say i run the code now at 3pm (15 hr) this will increment it to 16 hundred hour...

Nugs

        int HourPeriodIndex = 1;
        if (DateTime.Now.Hour % 2 != 0)
        {
            HourPeriodIndex = DateTime.Now.Hour + 1;
        }
        else
        {
            HourPeriodIndex = DateTime.Now.Hour;
        }

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21808820
Try this:
0
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 300 total points
ID: 21808829
Ah! It lost my code snippet!  This should work because the interger division will truncate any decimal before multiplying by 2 again:
int HourPeriod = ( DateTime.Now.Hour / 2 ) * 2;

Open in new window

0
 
LVL 11

Assisted Solution

by:Mihai Stancescu
Mihai Stancescu earned 100 total points
ID: 21819522
You can try this:

Hope this helps!

Regard,
Mishu
ArrayList HourPeriods = new ArrayList(new int[12] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 });
 
            int hour = DateTime.Now.Hour;
            int theIndex = -1;
            try
            {
                if (HourPeriods.Contains(hour))
                {
                    theIndex = HourPeriods.IndexOf(hour);
                }
            }
            catch (Exception)
            {
                theIndex = HourPeriods.IndexOf(hour + 1);
            }

Open in new window

0
 
LVL 2

Author Closing Comment

by:Nugs
ID: 31468160
Thanks everyone. I eventually opted for jcoehoorn simpler solution here, but none of the solutions posted were wrong by any means so i split the points... Thanks for all the input.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question