C# while loop involving DateTime variable not performing calculations -


i'm working on project utilizes datetime variables find cost of travel between variety of dates. of click event fire calculations total price , average price won't run. appreciated.

here's code:

private void btncalculate_click(object sender, eventargs e)         {             datetime arrival = datetime.parse(txtarrivaldate.text);             datetime departure = datetime.parse(txtdeparturedate.text);                                   timespan numdays = departure - arrival;             int ndays = numdays.days;             txtnights.text = ndays.tostring();             decimal totalprice = 0;              txttotalprice.text = totalprice.tostring("c");             decimal avprice = totalprice / ndays;              txtavgprice.text = avprice.tostring("c");             datetime nighttocharge = arrival;             decimal pricepernight = 0m;             int = ndays;             while (i > 0)             {                 dayofweek dayofweek = nighttocharge.dayofweek;                 if (dayofweek == dayofweek.friday ||                     dayofweek == dayofweek.saturday)                 {                     pricepernight = 150m;                 }                 else                 {                     pricepernight = 120m;                  }                 i--;                 nighttocharge = nighttocharge.adddays(1);                 totalprice += pricepernight;             }          } 

your prices aren't being shown because you're setting value of textbox text before calculate price. you're setting average price before calculate total.

just calculate average , assign price values after while loop:

while (i > 0) {     // code omitted brevity }  decimal avprice = totalprice / ndays; txttotalprice.text = totalprice.tostring("c"); txtavgprice.text = avprice.tostring("c"); 

Comments