DateTime equivalent struct with start/end properties

  • Thread starter Thread starter Anders Borum
  • Start date Start date
A

Anders Borum

Hello!

As part of a refactoring iteration, I was looking at consolidating two
properties (Start and EndDate) to a single structure that would allow a
single access point for the "duration" (so to speak).

I considered using the TimeSpan, but that structure doesn't provide the
boundaries for the "duration", only the length of the "duration" itself.

The obvious step next would be creating my own structure that provided these
boundaries - except that I have a feeling this is already present in the
framework (or sounds like it should be). Am I looking for something that
doesn't exist? :)

Thanks in advance!
 
Anders,

Why not something like this:

public struct Duration
{
public DateTime Start;
public DateTime End;
}

Hope this helps.
 
In .Net 2.0 framework, try the System.Diagnostics.StopWatch Class

That just gives an elapsed time as far as I can see - and not one that
you can change manually.
 
Hello!

I already use the StopWatch class in another part of the application and I
don't think using this type as a public property on types that will be
instantiated really often (i.e. thousands).

The implementation used a start / end date, but businesslogic implied that
the starting date should be less than the ending date (obviously). This
caused a validation exception to be thrown if the programmer was assigning a
startdate greater than enddate and so on .. as part of setting i.e. a future
duration.

So I wrote some sample lines of C# to see, how the API would make the most
sense in client code (the Brad Abrams way), and decided to change the
interface.

A dedicated structure to handle this is probably the way to go. I was
looking for a native BCL class, but will probably have to wait until 3.0 to
see this :)

Thanks for the input!
 
Great!

I'll get the code and see what you've been implemented. However, we've
already created a custom structure here that works quite well but it's
always interesting to see other solutions.

Thanks for all the input.
 
Anders,
In addition to the other comments.

I have a Range(Of T), DateRange & TimeRange available at:

http://www.tsbradley.net/Cookbook/Generics/genericRange.aspx
http://www.tsbradley.net/Cookbook/Patterns/dateRange.aspx
http://www.tsbradley.net/Cookbook/Patterns/timeRange.aspx

One should be able to easily convert the above to C# if needed.

My DateRange will ignore the respective Time parts of the DateTime values,
while the TimeRange will ignore the respective Date parts, plus it handles
ranges spanning midnight.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hello!
|
| As part of a refactoring iteration, I was looking at consolidating two
| properties (Start and EndDate) to a single structure that would allow a
| single access point for the "duration" (so to speak).
|
| I considered using the TimeSpan, but that structure doesn't provide the
| boundaries for the "duration", only the length of the "duration" itself.
|
| The obvious step next would be creating my own structure that provided
these
| boundaries - except that I have a feeling this is already present in the
| framework (or sounds like it should be). Am I looking for something that
| doesn't exist? :)
|
| Thanks in advance!
|
| --
| With regards
| Anders Borum / SphereWorks
| Microsoft Certified Professional (.NET MCP)
|
|
 
Back
Top