I'm looking for an efficient way to do a process (Process1) every N times of time period, as for example every 2 weeks or every 6 months, being the integer value (N) defined by the user who can enter any integer value from the keyboard.
For now, I have the code doing the ‘Process1’ as should when this process needs to be done every 1 week or every 1 month, so it is working perfectly until this point.
For the N time periods part, I’m trying with something based on the next logic with these if-statements and a counter that are inside a predefined loop that is already working as should and can't be modified, so the only part I need to get is the one of what would be inside the loop.
…
private int N; //Integer number defined by the user
private int i = 1; //Counter
…
// loop
{
…
If ( N == 1 )
{
/*
N = 1 would be the default value with which: 1 week (every 1 week),
or 1 month (every 1 month),…
*/
…
//do Process1 /*
This part is already working as should because is not even needed to specify
there is a `N == 1` to have Process1 to be done every week or every month, but
I put it as logic guide of how I think it would be the process flow.
*/
}
else if ( N > 1 && i < N )
{
i++; //Increment the counter
}
If ( i == N )
{
…
//do Process1
i = 1;
}
…
}
…
The efficiency issue I can see with this kind of logic is that if N = 40 (40 weeks for example), or a number even larger due a typing error, then the code for ‘i++’ process will be done 40 times before to reach the point to do Process1, and that’s the reason why I was wondering if there is a more efficient or simplified way to maybe just specify something similar like:
…
//To specify Process1 to be done every N weeks, like every 2 weeks, or any other N int number.
If ( ( Period.DayOfWeek == DayOfWeek.Friday) *N )
{
…
//do Process1
}
…
//To specify Process1 to be done every N months, like every 6 months, or any other N int number.
If ( ( Period == Time.Month) *N )
{
…
//do Process1
}
Some clarifications:
Please note this is needed to work with historical data and there is no need to change the current way with which Process1 is done.
Also, because the requirements, it can’t be done with libraries in general or with external programs, neither in a way to filter the historical data with some other ways.
What can be used is only basic code like if-statements
, DateTime/DayOfWeek
, counters
and these kind of “basic code”, among other things because it is a very simple situation that doesn’t need any special “feature”. Please think that everything is working as should 100% perfect. And the only part that can be modified is the DateTime/DayOfWeek part in order to be able to just change the current way: “do somthing every week/month” to the required way: “do somthing every N weeks/months”. Only that. Thank you for every comment trying to provide alternatives, but it's a very basic situation and only the DateTime
part can be modified.
Thank you in advance for the help!