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?
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.
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.
end edit.
B-) Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
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 :
Using the Minutes property (which is limited from 0-59) instead of TotalMinutes (which is a sum of the total minutes including hours)
Using a SQL CHECK Constraint to limit your value range at the database level.
Using the modulo operator '%' to ensure that even values over 60 would always be less than 60 (uses equal divisions of 60 and modulo yields the remainder)
Minutes Approach
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;
CHECK Constraint
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.
Modulo Operator
Additionally, you could always take your value and use the modulo '%' operator to ensure that your value would never exceed 60 :
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.
B-) Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
Member
94 Points
278 Posts
limiting variable
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?
All-Star
37441 Points
9076 Posts
Re: limiting variable
Feb 13, 2015 06:31 AM|AidyF|LINK
Star
14297 Points
5797 Posts
Re: limiting variable
Feb 13, 2015 09:15 AM|gerrylowry|LINK
@mdehghani
i've zero doubt that AidyF intended:
however, i strongly recommend that you avoid var whenever possible, i.e., use the exact Type:
if you're uncertain about the Type, you can do this:
intellisense also will often tell you the Type, as will MSDN:
https://msdn.microsoft.com/en-us/library/system.timespan.totalminutes(v=vs.110).aspx
edit:
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.
end edit.
All-Star
114593 Points
18503 Posts
MVP
Re: limiting variable
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 :
Minutes Approach
By changing your TotalMinutes property to Minutes, you'll only get the current number of minutes (ignoring hours) for your TimeSpan :
CHECK Constraint
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 :
I haven't worked with these extensively, but they might be looking into.
Modulo Operator
Additionally, you could always take your value and use the modulo '%' operator to ensure that your value would never exceed 60 :
which would just look like :
This will likely be equivalent to the first approach.
Member
94 Points
278 Posts
Re: limiting variable
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..
Star
14297 Points
5797 Posts
Re: limiting variable
Feb 13, 2015 12:32 PM|gerrylowry|LINK
@Rion William...
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.
output:
Rion, in this scenario, we cheat our employed out of 38 minutes because we would pay for 22 minutes instead of 60.
similar to the above:
result is 22 instead of 82.
@mdehghani
Caution: the result will not always be an event number of minutes because duration is a Double.
output: