Optional parameter in Date Function

  • Thread starter Thread starter John Morgan
  • Start date Start date
J

John Morgan

Does anyone know what parameter should be used instead of Date = 0 for the
optional parameter in the following function?

Public Function dhAge(ByVal dtmBD As Date, Optional ByVal dtmDate As Date =
0) As Short
 
John Morgan said:
Does anyone know what parameter should be used instead of Date = 0
for the optional parameter in the following function?

Public Function dhAge(ByVal dtmBD As Date, Optional ByVal dtmDate As
Date = 0) As Short

Which date should be the default value when dtmDate is not passed? Zero is
not a date.

You might also consider writing an overloaded version instead.
 
Public Function dhAge(ByVal dtmBD As Date, Optional ByVal dtmDate As Date =
0) As Short

Dim intAge As Short

If dtmDate = System.DateTime.FromOADate(0) Then

' Did the caller pass in a date? If not, use

' the current date.

dtmDate = Today

End If

intAge = DateDiff(Microsoft.VisualBasic.DateInterval.Year, dtmBD, dtmDate)

If dtmDate < DateSerial(Year(dtmDate), Month(dtmBD), Day(dtmBD)) Then

intAge = intAge - 1

End If

dhAge = intAge

Return dhAge

End Function



--- This works as is in vb6
 
John Morgan said:
Public Function dhAge(ByVal dtmBD As Date, Optional ByVal dtmDate As
Date = 0) As Short

Dim intAge As Short

If dtmDate = System.DateTime.FromOADate(0) Then

' Did the caller pass in a date? If not, use

' the current date.

dtmDate = Today

End If

intAge = DateDiff(Microsoft.VisualBasic.DateInterval.Year, dtmBD,
dtmDate)

If dtmDate < DateSerial(Year(dtmDate), Month(dtmBD), Day(dtmBD))
Then

intAge = intAge - 1

End If

dhAge = intAge

Return dhAge

End Function



--- This works as is in vb6


What's your question?

My question is still: Which date do you expect to be passed whenever you
don't pass the second argument?

Maybe you are looking for this (untested):

Public Function dhAge(ByVal dtmBD As Date) As Short
Return dhAge(dtmBD, Date.Today)
End Function

Public Function dhAge( _
ByVal dtmBD As Date, _
ByVal dtmDate As Date) As Short

Dim intAge As Short

intAge = DateDiff(DateInterval.Year, dtmBD, dtmDate)

If dtmDate < New Date(dtmDate.Year, dtmBD.Month, dtmBD.Day) Then
intAge -= 1
End If

Return intAge
End Function
 
The first thing your function does is check whether dtmDate was input by the
user and set it to Todays Date if it is not. Therefore, instead of the
optional parameter being 0 it should be Date.Today. However, this is not
valid as it is not a constant. You are now programming in VB.Net not VB6 and
so you should not expect all your VB6 code to work as it did. Instead of
adding Optional parameters you simply create multiple functions with
different parameters. This is what Armin suggested, it is called
Overloading.

The VB.Net translation of your VB6 code follows:

Public Function dhAge(ByVal dtmBD As Date) As Short
Return dhAge(dtmBD, Date.Today)
End Function

Public Function dhAge(ByVal dtmBD As Date, ByVal dtmDate As Date) As Short
Dim shAge As Short _
= CShort(DateDiff(DateInterval.Year, dtmBD, dtmDate))
If dtmDate < DateSerial(dtmDate.Year, dtmBD.Month, dtmBD.Day) Then
shAge -= CShort(1)
End If
Return shAge
End Function
 
"Mick Doherty"
Look at that. Great minds think alike, only some have Option Strict
On. ;-)

*argh* It is *always* on! :-)
Unfortunatelly I tried the code in a project just upgraded by the upgrade
wizard, and there is option strict off.

Improved version: ;-)

Public Function dhAge(ByVal dtmBD As Date) As Short
Return dhAge(dtmBD, Date.Today)
End Function

Public Function dhAge( _
ByVal dtmBD As Date, _
ByVal dtmDate As Date) As Short

Dim intAge As Short

intAge = CShort(DateDiff(DateInterval.Year, dtmBD, dtmDate))

If dtmDate < New Date(dtmDate.Year, dtmBD.Month, dtmBD.Day) Then
intAge -= 1S
End If

Return intAge
End Function
 
This does not compile with Option Strict On

Use always Strict on

That Mick showed it us: greath!!!!.

I hope that Nick and Fergus see this also.

:-)))))))))))) 'and not more than that

Cor
 
Cor said:
This does not compile with Option Strict On

Use always Strict on

I always do - blame the upgrade wizard. ;-)))
That Mick showed it us: greath!!!!.

I hope that Nick and Fergus see this also.

:-)))))))))))) 'and not more than that

Cor

Strange..... Each time I read your name I see a girl. *g*

"Hit me baby one more time"

;-))))))))
 
Yes, overloading was the answer -- I had been reading about that, -- I am
trying to make a class in vb.net and process it as unmanaged code -- My
first step in converting a 300,000 line vb6 program to vb.net.
Thank's
"Mick Doherty"
 
Sounds like you've got a lot of re-learning to do. I remember when I first
leapt from VB6 to .net, back in January this year, I thought I was never
going to get the hang of it. However after about 3 weeks I was almost as
confident in .net as I was in VB6. That said there is still a lot for me to
learn.
Have fun, and any questions just ask.

Mick Doherty
 
Back
Top