locked
limiting variable RRS feed

  • Question

  • User-2060576634 posted

    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?

    var duration=(currentdatetime-userstarttime).TotalMinutes;
    
    Friday, February 13, 2015 6:10 AM

Answers

  • User-760709272 posted
    var duration=(currentdatetime-userstarttime).TotalMinutes;
    
    if (var > 60)
    {
        // show an error message to the user
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2015 6:31 AM
  • User-434868552 posted

    @mdehghani

    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:

    Console.WriteLine (duration.GetType());

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2015 9:15 AM
  • User281315223 posted

    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 :

    # Input | Input % 60
          0            0
         15           15
         45           45
         59           59
         60            0
         73           13
         90           30                                      

    which would just look like :

    var duration = (currentdatetime-userstarttime).TotalMinutes % 60;

    This will likely be equivalent to the first approach.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2015 9:31 AM

All replies

  • User-760709272 posted
    var duration=(currentdatetime-userstarttime).TotalMinutes;
    
    if (var > 60)
    {
        // show an error message to the user
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2015 6:31 AM
  • User-434868552 posted

    @mdehghani

    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:

    Console.WriteLine (duration.GetType());

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2015 9:15 AM
  • User281315223 posted

    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 :

    # Input | Input % 60
          0            0
         15           15
         45           45
         59           59
         60            0
         73           13
         90           30                                      

    which would just look like :

    var duration = (currentdatetime-userstarttime).TotalMinutes % 60;

    This will likely be equivalent to the first approach.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2015 9:31 AM
  • User-2060576634 posted

    thanks all for your responses.. I think the main idea is to use an if,else statement.. this does the trick indeed.. 

    Friday, February 13, 2015 10:14 AM
  • User-434868552 posted

    @Rion William...

    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;

    output:

    End:    2015-02-13 13:37:41
    Start:  2015-02-13 12:15:41
    22
    82

    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.

    @mdehghani

    Caution:  the result will not always be an event number of minutes because duration is a Double.

    Double durationTotalMinutes = ((currentDateTime - userStartTime).TotalMinutes);

    output:

    End:    2015-02-13 13:51:30
    Start:  2015-02-13 12:29:08
    82.3666666666667



    Friday, February 13, 2015 12:32 PM