Long Arithmetic not elegant in .NET

  • Thread starter Thread starter Phillip Taylor
  • Start date Start date
P

Phillip Taylor

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
 
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*
 
The first code snippet works fines here ??? (.NET 2.0). How do you see the
value for ans ?
 
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.
 
Works fine here, with option strict both on and off.

Tom Dacon
Dacon Software Consulting
 
Phillip Taylor said:
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.
 
Back
Top