Compare Timespan Values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russ Green

    Compare Timespan Values

    How does this:
    public TimeSpan Timeout
    {
    get { return timeout; }
    set
    {
    timeout = value;
    if(timeout < licenseTimeout)
    licenseTimeout = timeout;
    }
    }
    private TimeSpan timeout = TimeSpan.FromMi nutes(10);

    Convert to VB.NET when in VB.NET Opertor '<' is not defined for types
    'Sysem.TimeSpan ' and System.TimeSpan '


    TIA

    Russ


  • Marinus Holkema

    #2
    RE: Compare Timespan Values

    You can use the CompareTo method of TimeSpan this 'will compare two TimeSpans

    "Russ Green" wrote:
    [color=blue]
    > How does this:
    > public TimeSpan Timeout
    > {
    > get { return timeout; }
    > set
    > {
    > timeout = value;
    > if(timeout < licenseTimeout)
    > licenseTimeout = timeout;
    > }
    > }
    > private TimeSpan timeout = TimeSpan.FromMi nutes(10);
    >
    > Convert to VB.NET when in VB.NET Opertor '<' is not defined for types
    > 'Sysem.TimeSpan ' and System.TimeSpan '
    >
    >
    > TIA
    >
    > Russ
    >
    >
    >[/color]

    Comment

    • Cor Ligthert

      #3
      Re: Compare Timespan Values

      Russ,

      Make it yourself easy, you can in datetime and timespan forever compare the
      ticks.

      However, not when it is a by by instance a by a datetimepicker set date,
      than you have to do it in another way.

      But in your case I think that it is the way to go.

      I hope this helps,

      Cor


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Compare Timespan Values

        Russ,
        Rather then ask specifically how to Compare two TimeSpan values I normally
        ask how to Compare any two values in .NET.

        Values that are comparable in .NET either override Object.Equals for
        identity Equals or implement the IComparable interface. Classes that
        override Object.Equals or Implement IComparable also normally override the
        =, <>, <, <=, >, and >= operators. Unfortunately VB.NET 2002 & 2003 do not
        directly support overloaded operators, instead you need to explicitly call
        the routine (in the case of = operator the routine is op_Equality, see
        "Operator Overloading Usage Guidelines" listed below. VS.NET 2005 (aka
        Whidbey, due out later in 2005, http://lab.msdn.microsoft.com/vs2005/) will
        support using & defining overloaded operators.

        So in the case of TimeSpan I can simply call TimeSpan.Compar e or
        TimeSpan.Compar eTo to compare TimeSpan Values.

        Using Equals, Compare, or CompareTo allows my code to be consistent across
        all types, rather then needing to remember what property or properties I
        need use for each type. For example, if I had a Person type I can simply
        call Person.Compare, rather then needing to remember what set of attributes
        (properties) of the Person class. Of course the danger of comparing sets of
        attributes/properties is code duplication, by calling Person.Compare the
        comparison itself is neatly encapsulated in the Person object. Further the
        Compare routine is polymorphic, in that each type has its own Compare... Of
        course with VS.NET 2005 we will have overloaded operators so I can simply
        use the overloaded operators...

        I would not rely on comparing Ticks or other properties as that IMHO is an
        "oddball solution", an "Oddball Solution" is when you have two or more
        similar constructs (comparing objects) & you do it two or more different
        ways (DateTime.Ticks , Person.Name). "Oddball Solution" is a "code smell"
        that is identified in "Refactorin g to Patterns"
        http://www.industriallogic.com/xp/refactoring/.

        There was a long discussion a few months back in this group on using =
        operators or the Equals method titled "Comparing Empty Value Types" from
        about November 17, 2004. See the entire thread starting with:




        For a list of commonly overloaded Operators & the alternative methods see
        "Operator Overloading Usage Guidelines" at:


        IComparable:


        Object.Equals:




        Hope this helps
        Jay

        "Russ Green" <mailNO@SPAMrus sgreen.com> wrote in message
        news:%2311CuGEX FHA.2768@tk2msf tngp13.phx.gbl. ..
        | How does this:
        | public TimeSpan Timeout
        | {
        | get { return timeout; }
        | set
        | {
        | timeout = value;
        | if(timeout < licenseTimeout)
        | licenseTimeout = timeout;
        | }
        | }
        | private TimeSpan timeout = TimeSpan.FromMi nutes(10);
        |
        | Convert to VB.NET when in VB.NET Opertor '<' is not defined for types
        | 'Sysem.TimeSpan ' and System.TimeSpan '
        |
        |
        | TIA
        |
        | Russ
        |
        |


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Compare Timespan Values

          Jay B. Harlow [MVP - Outlook] <Jay_Harlow_MVP @msn.com> wrote:[color=blue]
          > Using Equals, Compare, or CompareTo allows my code to be consistent across
          > all types, rather then needing to remember what property or properties I
          > need use for each type.[/color]

          Does that mean you use them for Int32 (etc) as well? If not, where do
          you draw the line?

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Compare Timespan Values

            Jon,
            Although I don't state it, I thought it was easily inferred:

            First choice is the = operator on the values themselves (not properties of
            the values). Seeing as VB.NET supports the = operator on both Int32 &
            DateTime, I use the = operator.

            Unfortunately VB.NET 2002 & 2003 does not support overloaded operators, so I
            "fall back" to the Equals method, rather then risk duplicate code by using
            one or more properties of the values. Of course in VB.NET 2005 overloaded
            operators are supported, ergo I will use the overloaded = operator. Unless I
            am working on a polymorphic routine where Object.Equals, IEquatable(Of T),
            IComparable, or IComparable(Of T) make more sense, then I would use one of
            these Interfaces or Object.Equals instead...

            Seeing as C# does support overloading the = operator, in C# I would favor
            the = operator...

            The point I am attempting to make is that IMHO its better to compare the
            values/objects themselves, rather then rely on comparing
            attributes/properties of the said values/objects.

            Hope this helps
            Jay

            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            news:MPG.1cf789 84e0be44e998c15 1@msnews.micros oft.com...
            | Jay B. Harlow [MVP - Outlook] <Jay_Harlow_MVP @msn.com> wrote:
            | > Using Equals, Compare, or CompareTo allows my code to be consistent
            across
            | > all types, rather then needing to remember what property or properties I
            | > need use for each type.
            |
            | Does that mean you use them for Int32 (etc) as well? If not, where do
            | you draw the line?
            |
            | --
            | Jon Skeet - <skeet@pobox.co m>
            | http://www.pobox.com/~skeet
            | If replying to the group, please do not mail me too


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Compare Timespan Values

              Doh!

              Of course types that don't actually override Object.Equals nor implement one
              of IEquatable(Of T), IComparable, or IComparable(Of T), nor overload any of
              the comparison operators then of course, as a last resort, I would compare
              specific attributes/properties. Especially is it was a type I could not
              modify... Of course I would consider encapsulating this comparison in a
              Comparer object (Singleton?) to minimize duplicate code...

              Jay

              "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
              news:uk5evuVXFH A.2664@TK2MSFTN GP15.phx.gbl...
              | Jon,
              | Although I don't state it, I thought it was easily inferred:
              |
              | First choice is the = operator on the values themselves (not properties of
              | the values). Seeing as VB.NET supports the = operator on both Int32 &
              | DateTime, I use the = operator.
              |
              | Unfortunately VB.NET 2002 & 2003 does not support overloaded operators, so
              I
              | "fall back" to the Equals method, rather then risk duplicate code by using
              | one or more properties of the values. Of course in VB.NET 2005 overloaded
              | operators are supported, ergo I will use the overloaded = operator. Unless
              I
              | am working on a polymorphic routine where Object.Equals, IEquatable(Of T),
              | IComparable, or IComparable(Of T) make more sense, then I would use one of
              | these Interfaces or Object.Equals instead...
              |
              | Seeing as C# does support overloading the = operator, in C# I would favor
              | the = operator...
              |
              | The point I am attempting to make is that IMHO its better to compare the
              | values/objects themselves, rather then rely on comparing
              | attributes/properties of the said values/objects.
              |
              | Hope this helps
              | Jay
              |
              | "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              | news:MPG.1cf789 84e0be44e998c15 1@msnews.micros oft.com...
              || Jay B. Harlow [MVP - Outlook] <Jay_Harlow_MVP @msn.com> wrote:
              || > Using Equals, Compare, or CompareTo allows my code to be consistent
              | across
              || > all types, rather then needing to remember what property or properties
              I
              || > need use for each type.
              ||
              || Does that mean you use them for Int32 (etc) as well? If not, where do
              || you draw the line?
              ||
              || --
              || Jon Skeet - <skeet@pobox.co m>
              || http://www.pobox.com/~skeet
              || If replying to the group, please do not mail me too
              |
              |


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Compare Timespan Values

                Jay B. Harlow [MVP - Outlook] <Jay_Harlow_MVP @msn.com> wrote:[color=blue]
                > Although I don't state it, I thought it was easily inferred:
                >
                > First choice is the = operator on the values themselves (not properties of
                > the values). Seeing as VB.NET supports the = operator on both Int32 &
                > DateTime, I use the = operator.[/color]

                But doesn't that then go against:

                <quote>
                Using Equals, Compare, or CompareTo allows my code to be consistent
                across all types, rather then needing to remember what property or
                properties I need use for each type.
                </quote>

                I could understand a pattern which said "for all the primitive types,
                use operators, but for all other types use Equals" but to use the
                operator for DateTime goes against that. If it's okay to remember
                DateTime, might there not be other value types it's worth remembering?
                [color=blue]
                > The point I am attempting to make is that IMHO its better to compare the
                > values/objects themselves, rather then rely on comparing
                > attributes/properties of the said values/objects.[/color]

                Sure.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Cor Ligthert

                  #9
                  Re: Compare Timespan Values

                  Jay,

                  In my opinin is this the same discussion as the toArgb discussion.

                  Although in this case is the difference in the problem, that your for the
                  colours very valid point, that you want be more clear in the code, about
                  what colours you are talking, is less important. You only want to know if
                  date "A" was before or after date "B" or that the time difference between
                  "A" and "B" was equal, higher or less from each other.

                  Just my opinion.

                  Cor


                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: Compare Timespan Values

                    Cor,
                    | In my opining is this the same discussion as the toArgb discussion.
                    Yes, that's why I referenced the ToArgb discussion.

                    Jay

                    "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
                    news:OUIBeYWXFH A.3584@TK2MSFTN GP14.phx.gbl...
                    | Jay,
                    |
                    | In my opinin is this the same discussion as the toArgb discussion.
                    |
                    | Although in this case is the difference in the problem, that your for the
                    | colours very valid point, that you want be more clear in the code, about
                    | what colours you are talking, is less important. You only want to know if
                    | date "A" was before or after date "B" or that the time difference between
                    | "A" and "B" was equal, higher or less from each other.
                    |
                    | Just my opinion.
                    |
                    | Cor
                    |
                    |


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: Compare Timespan Values

                      Jon,
                      Not really:

                      The pattern (I use) is "For all types that support operators use operators,
                      but for all other types use Equals...".

                      Although I didn't state it, my original post was in the context of the
                      operators are not available, as the original poster indicated the operators
                      are not available. So I don't see any conflict. Looking back I probably
                      should have included that in my original post.

                      Hope this helps
                      Jay


                      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                      news:MPG.1cf82e a7b6cbadb998c15 c@msnews.micros oft.com...
                      | Jay B. Harlow [MVP - Outlook] <Jay_Harlow_MVP @msn.com> wrote:
                      | > Although I don't state it, I thought it was easily inferred:
                      | >
                      | > First choice is the = operator on the values themselves (not properties
                      of
                      | > the values). Seeing as VB.NET supports the = operator on both Int32 &
                      | > DateTime, I use the = operator.
                      |
                      | But doesn't that then go against:
                      |
                      | <quote>
                      | Using Equals, Compare, or CompareTo allows my code to be consistent
                      | across all types, rather then needing to remember what property or
                      | properties I need use for each type.
                      | </quote>
                      |
                      | I could understand a pattern which said "for all the primitive types,
                      | use operators, but for all other types use Equals" but to use the
                      | operator for DateTime goes against that. If it's okay to remember
                      | DateTime, might there not be other value types it's worth remembering?
                      |
                      | > The point I am attempting to make is that IMHO its better to compare the
                      | > values/objects themselves, rather then rely on comparing
                      | > attributes/properties of the said values/objects.
                      |
                      | Sure.
                      |
                      | --
                      | Jon Skeet - <skeet@pobox.co m>
                      | http://www.pobox.com/~skeet
                      | If replying to the group, please do not mail me too


                      Comment

                      Working...