Last post Feb 13, 2015 12:32 PM by gerrylowry
Feb 13, 2015 06:10 AM|mdehghani|LINK
hi everybode.. can I put a maximum limit for this variable? I don't want it to exceed 60 minutes.. i want to insert it to a table but i don't want a number larger than 60.. is it possible?
Feb 13, 2015 06:31 AM|AidyF|LINK
if (var > 60)
// show an error message to the user
Feb 13, 2015 09:15 AM|gerrylowry|LINK
i've zero doubt that AidyF intended:
if(duration > 60)
however, i strongly recommend that you avoid var whenever possible, i.e., use the exact Type:
Double duration = (curretnDateTime - userStartTime).TotalMinutes;
if you're uncertain about the Type, you can do this:
intellisense also will often tell you the Type, as will MSDN:
Note: as AidyF has shown, the way that one generally limits a variable is to constrain it in some way, for example, as AidyF has
done, the value is upper range verified via an if statement.
values like Double have limits too, for example https://msdn.microsoft.com/en-us/library/system.double.maxvalue(v=vs.110).aspx ...
in your specific O.P. case, it's unlikely you'll ever come close to exceeding a limit -- however, in cases where a value may exceed its upper or lower limit, you need some form of prophylactic, for example, a try/catch.
Feb 13, 2015 09:31 AM|Rion Williams|LINK
The if-statement that AidyF provided would probably be the most straight-forward way of handling this and it's fairly easy to implement and read.
There are a few other things you could consider as well :
By changing your TotalMinutes property to Minutes, you'll only get the current number of minutes (ignoring hours) for your TimeSpan :
var duration = (currentdatetime-userstarttime).Minutes;
Another approach if you wanted to handle this at the server-side level would be to place a
CHECK constraint on your specific value within the database to ensure that your value is less than 60 :
CREATE TABLE YourTable(
Duration INT NOT NULL CHECK (Duration <= 60)
I haven't worked with these extensively, but they might be looking into.
Additionally, you could always take your value and use the modulo '%' operator to ensure that your value would never exceed 60 :
# Input | Input % 60
which would just look like :
var duration = (currentdatetime-userstarttime).TotalMinutes % 60;
This will likely be equivalent to the first approach.
Feb 13, 2015 10:14 AM|mdehghani|LINK
thanks all for your responses.. I think the main idea is to use an if,else statement.. this does the trick indeed..
Feb 13, 2015 12:32 PM|gerrylowry|LINK
Using the Minutes property
Rion, AFAIK, this does not give the answer wanted by mdehghani; i.e., i ASSuME that mdehghani wants
to exclude results > 60 minutes.
Here's a scenario where that might be the case: at hypothetical company x, overtime is allowed without management approval, only if it does not exceed one hour.
Double durationMinutes = (currentDateTime - userStartTime).Minutes;
Double durationTotalMinutes = (currentDateTime - userStartTime).TotalMinutes;
End: 2015-02-13 13:37:41
Start: 2015-02-13 12:15:41
Rion, in this scenario, we cheat our employed out of 38 minutes because we would pay for 22 minutes instead of 60.
Using the modulo operator '%'
similar to the above:
Double durationTotalMinutes = ((currentDateTime - userStartTime).TotalMinutes % 60);
result is 22 instead of 82.
Caution: the result will not always be an event number of minutes because duration is a
Double durationTotalMinutes = ((currentDateTime - userStartTime).TotalMinutes);
End: 2015-02-13 13:51:30
Start: 2015-02-13 12:29:08