Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi just beginner user in C#. Can i use for loops at below code? How can i do that?
I want to calculate for 7 days a week.

What I have tried:

C#
DateTime dt_startDay1 = Worktime_dtpicker_startDay1.Value;
DateTime dt_endDay1 = Worktime_dtpicker_endDay1.Value;
int dt_breakDay1 = Int32.Parse(Worktime_numUpDown_breakDay1.Value.ToString());

int diff_Day1 = (int)(dt_endDay1 - dt_startDay1).TotalMinutes;
int total_wtDay1 = diff_Day1 - dt_breakDay1;
label1.Text = Convert.ToString(total_wtDay1);

DateTime dt_startDay2 = Worktime_dtpicker_startDay2.Value;
DateTime dt_endDay2 = Worktime_dtpicker_endDay2.Value;
int dt_breakDay2 = Int32.Parse(Worktime_numUpDown_breakDay2.Value.ToString());

int diff_Day2 = (int)(dt_endDay2 - dt_startDay2).TotalMinutes;
int total_wtDay2 = diff_Day2 - dt_breakDay2;
label2.Text = Convert.ToString(total_wtDay2);
Posted
Updated 31-Mar-16 22:04pm
v2

Not really - a loop is good at doing "the same stuff over and over", but it needs to work with something that iterates; something that goes up or down by the same amount each time round.
And your code doesn't do that it - just accesses variables with names that change, which is very different.
Instead, I'd write a method which accepted two DateTime values and an integer, and returned an integer of string result. That way, you don't repeat the same code, just pass the different values, and store the result in the right place.
 
Share this answer
 
Comments
Member 12421315 31-Mar-16 18:18pm    
I'm sorry could you please furthermore explain.
MarcusCole6833 31-Mar-16 23:08pm    
All you are doing is collecting data the start time end time and break time, you need to create a function that accepts the end start and break times and then returns the total as a string.
private string total(x, y , z )

{
x - y - z = t;
return cstr(t);
}.

You can, but (no offense) that's a bit more complicated than I feel you're used to. And doing it the no-loop-boring-type-it-all way would be faster anyway.

However, if you want to try it, you have to store Worktime_dtpicker_startDay1, Worktime_dtpicker_startDay2, ..., WorkTime_dtpicker_startDay7 in a list:
C#
List<datetimepicker> startPickers = new List<datetimepicker>()
{
    Worktime_dtpicker_startDay1, Worktime_dtpicker_startDay2,
    Worktime_dtpicker_startDay3, Worktime_dtpicker_startDay4,
    Worktime_dtpicker_startDay5, Worktime_dtpicker_startDay6,
    Worktime_dtpicker_startDay7
}
Same for the end time pickers. And again the same for the break UpDowns. And for the result labels.
Then, you can
C#
for(int i = 0; i < 7; i++)
{
    int minutes = (int)((endPickers[i] - startPickers[i]).TotalMinutes);
    minutes -= breakUpDowns[i].Value;
    resultLabels[i].Value = minuts.ToString();
}

---------------------------------------------------------------
Level 2:
Totally not what you were asking, but read up on UserControls and create one that includes pickers and label. Then use 7 of them instead of all the individual controls. Calculate result within the usercontrol on every input change. No need for a loop at all, but gives a very responsive UI.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900