Re: Looking for some logic help with dates and working outs.

Tech-Archive recommends: Fix windows errors by optimizing your registry




"Kardon Coupé" <prefer.to@xxxxxxxxxxxxxxxxx> wrote

I would probably just use an Array of Dates and VB's Date functions to
manage the changes.

I've thought about that, as there are alot of code snippets around the web,
showing how to work out if a leap year and such, but I really, really don't
want to be re-arranging and completly changing my code...

I would like to keep the exsist structure, and work from that.

You could still use a date array and keep your 'structure', but it
would involve indirection, and a few assumptions. On the plus
side, you'd save on memory, and file space if you chose to save
that data to the disk, and you'd get the proper date calculations for
free....

Instead of using multiple arrays, you could substitute in properties for
the array names, to keep your other code as it is. If handled properly,
you could still use statements like:

Y = E_Year(Index)
E_Day(Index) = 21
E_Minute(Index) = E_Minute(Index) + 15
(What other 'structure' are you using?)

The code accessing the data via a Date variable would automatically
account for roll-overs from minutes to hours and days to months (etc.)
including short months and leap years.

The indirection comes in, when you access your properties, you actually
extract the desired value from the date value. The assumptions happen
when you try to assign a value to one of the properties. If you assign a
value to E_Day, the assumption is made that the new value represents
days. Or if you assign a value to E_Minute, the assumtion is made that
the new value represents minutes. (et al.)

Just to keep it neat and clean you could put that in a separate (standard)
module which would contain code like the following; (only E_Day and
E_Minute are shown, others would be similar...)

Try it and see....
LFS


Option Explicit
Private E_Data(1 To 5000) As Date

Public Property Get E_Day(ByVal Index As Long) As Long
If ValidAccess(Index) Then
' Retrieve day value
E_Day = Day(E_Data(Index))
End If
End Property

Public Property Let E_Day(ByVal Index As Long, ByVal Value As Long)
Dim prev As Long
If ValidAccess(Index) Then
' Assign Day value
prev = Day(E_Data(Index))
E_Data(Index) = DateAdd("d", Value - prev, E_Data(Index))
End If
End Property

Public Property Get E_Minute(ByVal Index As Long) As Long
If ValidAccess(Index) Then
' Retrieve minute value
E_Minute = Minute(E_Data(Index))
End If
End Property

Public Property Let E_Minute(ByVal Index As Long, ByVal Value As Long)
Dim prev As Long
If ValidAccess(Index) Then
' Assign minute value
prev = Minute(E_Data(Index))
E_Data(Index) = DateAdd("n", Value - prev, E_Data(Index))
End If
End Property

Private Function ValidAccess(Index As Long) As Boolean
ValidAccess = (Index >= LBound(E_Data) And Index <= UBound(E_Data))
End Function



.