Solved

Casting from Long to TimeSpan in vb.net

Posted on 2011-09-05
3
593 Views
Last Modified: 2012-05-12
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
Comment
Question by:Jedeye
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
x77 earned 334 total points
ID: 36483056
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
 
LVL 15

Assisted Solution

by:x77
x77 earned 334 total points
ID: 36483099
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
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 166 total points
ID: 36483113
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now