In vb6 I could say
If Time >#4:00:00 PM# And Time < #4:01:00 PM# Then
'Do Something
End If
Well, I don't see the Time function in vb.net. I have
experimented with the TimeSpan object for capturing
durations of things. And I tried the following with no
luck
If Now.TimeOfDay > #16:00:00# And Now.TimeOfDay <
#16:01:00# Then...
But got the error message that "<" and ">" are not defined
for a timespan. How can I perform the vb6 operation above
in vb.net?
Thanks
http://www.martinfowler.com/ap2/range.html
> If Now.TimeOfDay > #16:00:00# And Now.TimeOfDay <
> #16:01:00# Then...
Dim range As New TimeRange(#16:00:00#, #16:01:00#)
If range.Contains(Now) Then
...
End If
Something like:
Public Structure TimeRange
' Store the range as TimeSpans as the comparisons are "easier"
Private ReadOnly m_start As TimeSpan
Private ReadOnly m_finish As TimeSpan
' used to handle special case of the range spanning midnight
Private ReadOnly m_midnight As Boolean
Public Sub New(ByVal start As DateTime, ByVal finish As DateTime)
m_start = start.TimeOfDay
m_finish = finish.TimeOfDay
m_midnight = (TimeSpan.Compare(m_start, m_finish) > 0)
End Sub
Public ReadOnly Property Start() As DateTime
Get
Return DateTime.MinValue.Add(m_start)
End Get
End Property
Public ReadOnly Property Finish() As DateTime
Get
Return DateTime.MinValue.Add(m_finish)
End Get
End Property
Public Function Contains(ByVal value As DateTime) As Boolean
Dim timeOfDay As TimeSpan = value.TimeOfDay
If m_midnight Then
Return TimeSpan.Compare(m_start, timeOfDay) <= 0 OrElse
TimeSpan.Compare(timeOfDay, m_finish) <= 0
Else
Return TimeSpan.Compare(m_start, timeOfDay) <= 0 AndAlso
TimeSpan.Compare(timeOfDay, m_finish) <= 0
End If
End Function
End Structure
NOTE: I've defined the time range as being inclusive of the end points.
Hope this helps
Jay
"SJ" <S...@discussions.microsoft.com> wrote in message
news:18e301c4abea$79686c00$a601...@phx.gbl...
If you want the end points to be exclusive, then you can change test on the
TimeSpan.Compare lines in TimeRange.Contains:
Something like (untested):
> Public Function Contains(ByVal value As DateTime) As Boolean
> Dim timeOfDay As TimeSpan = value.TimeOfDay
> If m_midnight Then
> Return TimeSpan.Compare(m_start, timeOfDay) < 0 OrElse
> TimeSpan.Compare(timeOfDay, m_finish) < 0
> Else
> Return TimeSpan.Compare(m_start, timeOfDay) < 0 AndAlso
> TimeSpan.Compare(timeOfDay, m_finish) < 0
> End If
> End Function
Basically the value returned from TimeSpan.Compare is:
- less then 0 if the first timespan is less then the second
- equal 0 if the first timespan equals the second
- greater then 0 if the first timespan is greater then the second
Hope this helps
Jay
"Jay B. Harlow [MVP - Outlook]" <Jay_Har...@msn.com> wrote in message
news:ephdg6%23qEH...@TK2MSFTNGP14.phx.gbl...
>.
>
I made TimeRange a Structure (VB6 Type) as I wanted it to:
- act like primitive types
- be immutable
- value semantics are desired
Its size should be under 16 bytes.
One feature that VB.NET has that VB6 also is missing are Shared methods. I
would consider adding a Shared Parsed method to TimeRange that is able to
take a string & convert it into a new TimeRange value. Then I would also
override the Object.ToString method in TimeRange to allow converting the
TimeRange into a readable format.
> I realize that var typing is way stronger in DotNet and
> this results in less ambiguity. Is this one of the
> benefits of DotNet for this situation?
I consider being able to create value types such as TimeRange a benefit.
http://martinfowler.com/ieeeSoftware/whenType.pdf
I also consider the full OO nature of .NET when creating classes a major
benefit, such as constructors, inheritance, polymorphism (both inheritance &
implements), encapsulation...
Hope this helps
Jay
"SJ" <S...@discussions.microsoft.com> wrote in message
news:053c01c4abf4$62cbf480$a301...@phx.gbl...
Many thanks for your help and explanations.
>.
>
Robin A. Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and
Microsoft Visual C# .NET - Step by Step" from Microsoft Press covers the how
of OOP, unfortunately not the why of OOP. I consider it a good entry level
book.
David West's book "Object Thinking" from Microsoft Press covers the why of
OOP and some of the intellectual how (at an abstract design level, not
implementation level as the first book). I consider this a more advanced
book.
Gang of Four's (GOF) book "Design Patterns - Elements of Reusable
Object-Oriented Software" from Addison Wesley, is IMHO a "must have" book
for the serious OO developer. Design Patterns provide programmers with a
convenient way to reuse object-origned code & concepts amount programmers
and across projects, offering easy, time-saving solutions to commonly
recurring problems in software design. The GOF are Erich Gamma, Richard
Helm, Ralph Johnson, and John Vlissides.
James W. Cooper's book "Visual Basic Design Patterns - VB 6.0 and VB.NET" is
also a "must have" book that is an excellent companion to the above GOF
book. Cooper's book gives the VB6 & VB.NET view of each pattern in the GOF
book.
Once you have a good solid handle on OO and Patterns I consider Martin
Fowler's two books "Refactoring" and "Patterns of Enterprise Application
Architecture" both from Addison Wesley to be informative reads.
I recently started reading Joshua Kerievsky's book "Refactoring to Patterns"
also from Addison Wesley which helps bridge the gap between the GOF Patterns
book & Martin's Refactoring book.
Another book I also recently starting reading is James W. Newkirk & Alexei
A. Vorontsov's book "Test-Driven Development in Microsoft .NET" which
explains a useful methodology (Test Driven Development or TDD) in the
context of .NET. I find that TDD simplifies the development process.
I'm sure I have some other books that I could recommend...
Yes, I am currently reading two books...
Hope this helps
Jay
"SJ" <S...@discussions.microsoft.com> wrote in message
news:405601c4ac7f$71682050$a501...@phx.gbl...
> Wow! That was definitely a clear explanation. I guess my
> issue is that I just don't have a broad enough of
> experience yet, even with vb6, to appreciate all of this
> functionality. Thus, I have to ask these questions
> (humbly) and just assimilate.
>
> Many thanks for your help and explanations.
>
>
<<snip>>