• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 669
  • Last Modified:

Casting from Long to TimeSpan in vb.net

In VB.net - I am trying to cast a Long datatype to TimeSpan - I cannot seem to do this using ctype or directcast - any ideas?
0
Jedeye
Asked:
Jedeye
  • 2
3 Solutions
 
x77Commented:
You can build a Date or a Timespan from a long, you do´nt need cast.
Date an TimeSpan have constructor from Long (Ticks).

   Dim MyDate as new Date(TicksValue)
   Dim MyTimeSpan as new TimeSpan(TicksValue)
0
 
x77Commented:
You can also adjust a TimeSpan or a Date variable from a long (TicksValue) using Ticks Property.

   myTimeSpan.Ticks = TiksValue.

You can´nt cast a timespan as it is a ValueType.
0
 
Jacques Bourgeois (James Burger)Commented:
Depends on what is in your Long: ticks, minutes, seconds, days, months...

Use the constructor that enables you to pass the information:

For ticks: Dim myTimeSpan As New TimeSpan(yourLong)
For minutes: Dim myTimeSpan As New TimeSpan(0, CInt(yourLong), 0)
For days: Dim myTimeSpan As New TimeSpan(CInt(yourLong),0,0,0)
For milliseconds: Dim myTimeSpan As New TimeSpan(0, 0, 0, 0, CInt(yourLong))
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now