Long Arithmetic not elegant in .NET

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phillip Taylor

    Long Arithmetic not elegant in .NET

    This doesn't work:

    Dim t As DateTime = DateTime.Now
    Dim a As Long = t.Ticks

    Dim t2 As DateTime = t.AddSeconds(1)
    Dim b As Long = t2.Ticks

    Dim ans As Long = b - a

    sets ans to 0 instead of 1 million. You have to do this:

    Dim t As DateTime = DateTime.Now
    Dim a As Long = t.Ticks

    Dim t2 As DateTime = t.AddSeconds(1)
    Dim b As Long = t2.Ticks

    Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))

    to get around it. Although is there a more elegant way?

    Phill
  • Phillip Taylor

    #2
    Re: Long Arithmetic not elegant in .NET

    On Mar 3, 6:20 pm, Phillip Taylor <Phillip.Ross.T ay...@gmail.com >
    wrote:
    This doesn't work:
    >
    Dim t As DateTime = DateTime.Now
    Dim a As Long = t.Ticks
    >
    Dim t2 As DateTime = t.AddSeconds(1)
    Dim b As Long = t2.Ticks
    >
    Dim ans As Long = b - a
    >
    sets ans to 0 instead of 1 million. You have to do this:
    >
    Dim t As DateTime = DateTime.Now
    Dim a As Long = t.Ticks
    >
    Dim t2 As DateTime = t.AddSeconds(1)
    Dim b As Long = t2.Ticks
    >
    Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))
    >
    to get around it. Although is there a more elegant way?
    >
    Phill
    instead of ten million*

    Comment

    • Patrice

      #3
      Re: Long Arithmetic not elegant in .NET

      The first code snippet works fines here ??? (.NET 2.0). How do you see the
      value for ans ?

      --
      Patrice

      "Phillip Taylor" <Phillip.Ross.T aylor@gmail.com a écrit dans le message de
      news: b7e51177-3d75-41e5-af87-ea649ce9e7ae...l egroups.com...
      This doesn't work:
      >
      Dim t As DateTime = DateTime.Now
      Dim a As Long = t.Ticks
      >
      Dim t2 As DateTime = t.AddSeconds(1)
      Dim b As Long = t2.Ticks
      >
      Dim ans As Long = b - a
      >
      sets ans to 0 instead of 1 million. You have to do this:
      >
      Dim t As DateTime = DateTime.Now
      Dim a As Long = t.Ticks
      >
      Dim t2 As DateTime = t.AddSeconds(1)
      Dim b As Long = t2.Ticks
      >
      Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))
      >
      to get around it. Although is there a more elegant way?
      >
      Phill

      Comment

      • kimiraikkonen

        #4
        Re: Long Arithmetic not elegant in .NET

        On Mar 3, 8:20 pm, Phillip Taylor <Phillip.Ross.T ay...@gmail.com >
        wrote:
        This doesn't work:
        >
        Dim t As DateTime = DateTime.Now
        Dim a As Long = t.Ticks
        >
        Dim t2 As DateTime = t.AddSeconds(1)
        Dim b As Long = t2.Ticks
        >
        Dim ans As Long = b - a
        >
        sets ans to 0 instead of 1 million. You have to do this:
        >
        Dim t As DateTime = DateTime.Now
        Dim a As Long = t.Ticks
        >
        Dim t2 As DateTime = t.AddSeconds(1)
        Dim b As Long = t2.Ticks
        >
        Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))
        >
        to get around it. Although is there a more elegant way?
        >
        Phill
        Tried "Dim ans As Long = b - a" and "Dim ans As Long = (DirectCast(b,
        Long) - DirectCast(a, Long))" and they both outputs the same: 10000000

        Couldn't notice a difference.

        Comment

        • Tom Dacon

          #5
          Re: Long Arithmetic not elegant in .NET

          Works fine here, with option strict both on and off.

          Tom Dacon
          Dacon Software Consulting

          "Phillip Taylor" <Phillip.Ross.T aylor@gmail.com wrote in message
          news:b7e51177-3d75-41e5-af87-ea649ce9e7ae@e3 1g2000hse.googl egroups.com...
          This doesn't work:
          >
          Dim t As DateTime = DateTime.Now
          Dim a As Long = t.Ticks
          >
          Dim t2 As DateTime = t.AddSeconds(1)
          Dim b As Long = t2.Ticks
          >
          Dim ans As Long = b - a
          >
          sets ans to 0 instead of 1 million. You have to do this:
          >
          Dim t As DateTime = DateTime.Now
          Dim a As Long = t.Ticks
          >
          Dim t2 As DateTime = t.AddSeconds(1)
          Dim b As Long = t2.Ticks
          >
          Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))
          >
          to get around it. Although is there a more elegant way?
          >
          Phill

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Long Arithmetic not elegant in .NET

            "Phillip Taylor" <Phillip.Ross.T aylor@gmail.com schrieb:
            This doesn't work:
            >
            Dim t As DateTime = DateTime.Now
            Dim a As Long = t.Ticks
            >
            Dim t2 As DateTime = t.AddSeconds(1)
            Dim b As Long = t2.Ticks
            >
            Dim ans As Long = b - a
            >
            sets ans to 0 instead of 1 million. You have to do this:
            >
            Dim t As DateTime = DateTime.Now
            Dim a As Long = t.Ticks
            >
            Dim t2 As DateTime = t.AddSeconds(1)
            Dim b As Long = t2.Ticks
            >
            Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))
            >
            to get around it. Although is there a more elegant way?
            Both code samples produce the same result.

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

            Comment

            • Phillip Taylor

              #7
              Re: Long Arithmetic not elegant in .NET

              On Mar 4, 2:09 am, "Herfried K. Wagner [MVP]" <hirf-spam-me-
              h...@gmx.atwrot e:
              "Phillip Taylor" <Phillip.Ross.T ay...@gmail.com schrieb:
              >
              >
              >
              This doesn't work:
              >
              Dim t As DateTime = DateTime.Now
              Dim a As Long = t.Ticks
              >
              Dim t2 As DateTime = t.AddSeconds(1)
              Dim b As Long = t2.Ticks
              >
              Dim ans As Long = b - a
              >
              sets ans to 0 instead of 1 million. You have to do this:
              >
              Dim t As DateTime = DateTime.Now
              Dim a As Long = t.Ticks
              >
              Dim t2 As DateTime = t.AddSeconds(1)
              Dim b As Long = t2.Ticks
              >
              Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))
              >
              to get around it. Although is there a more elegant way?
              >
              Both code samples produce the same result.
              >
              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
              Hmmm I am unable to replicate this bug today...

              :-s

              Comment

              Working...