By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Optional parameter in Date Function

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"John Morgan" <j.******@premsoftware.com> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2

P: n/a
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:#$**************@tk2msftngp13.phx.gbl...
"John Morgan" <j.******@premsoftware.com> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3

P: n/a
"John Morgan" <j.******@premsoftware.com> schrieb
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
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4

P: n/a
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

"John Morgan" <j.******@premsoftware.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:#$**************@tk2msftngp13.phx.gbl...
"John Morgan" <j.******@premsoftware.com> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


Nov 20 '05 #5

P: n/a
Look at that. Great minds think alike, only some have Option Strict On. ;-)

"Armin Zingler" <az*******@freenet.de> wrote in message
news:ef**************@TK2MSFTNGP11.phx.gbl...
"John Morgan" <j.******@premsoftware.com> schrieb
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
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6

P: n/a
"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]>
schrieb
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

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7

P: n/a
Cor
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
Nov 20 '05 #8

P: n/a
"Cor" <no*@non.com> schrieb
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"

;-))))))))
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #9

P: n/a
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"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:e4**************@TK2MSFTNGP09.phx.gbl...
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

"John Morgan" <j.******@premsoftware.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:#$**************@tk2msftngp13.phx.gbl...
"John Morgan" <j.******@premsoftware.com> schrieb
> 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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html



Nov 20 '05 #10

P: n/a
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

"John Morgan" <j.******@premsoftware.com> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...
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"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in message news:e4**************@TK2MSFTNGP09.phx.gbl...
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

"John Morgan" <j.******@premsoftware.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:#$**************@tk2msftngp13.phx.gbl...
> "John Morgan" <j.******@premsoftware.com> schrieb
> > 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.
>
>
> --
> Armin
>
> http://www.plig.net/nnq/nquote.html
> http://www.netmeister.org/news/learn2quote.html
>



Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.