Loki70


I have a function that converts a date into a date "code" for my customer (i.e. January = "A", February = "B", etc). Ideally, I want the function to convert a passed date, but if no date was passed, just use today's date. I started with this:

Public Function DateCode(Optional ByVal DateToCalc As Date = Nothing) As String

...but the problem is that when I don't pass anything, the date is still set to "something", namely: " #12:00:00 AM#". I can't set the default value for the optional variable DateToCalc to "Now.Date", as it gives me an error "Constant expression is required". I tried to verify that this was not a valid date like this:

If Not IsDate(DateToCalc) Then DateToCalc = Now

..but this is still "OK", because it is technically still a date/time. I realize that I am probably missing something simple [stupid] and I would appreciate any suggestions.

Thank you.



Re: Setting Today's Date by Default in a Function

TaDa


Public Function DateCode() as string

return DateCode(now())

End Function

Public Function DateCode(D as date) as String

'Code here

Return Whatever

End Function





Re: Setting Today's Date by Default in a Function

Loki70

Thanks for replying, TaDa. Unfortunately, I don't think you understand fully what I am attempting to do.

I need to do two things in the one function:

  1. If I pass a date to the function, return it's date code.
  2. If I pass Nothing to the function, return Today's date code.

I can see from your code how I can do either in two separate functions, but I want to do both. Any further suggestions

Thank you again!






Re: Setting Today's Date by Default in a Function

TaDa

>Any further suggestions

Now that you're at the water, drink, drink!! ;-)





Re: Setting Today's Date by Default in a Function

PEng1

Any reason why you can't do this

Private Function DateCode(Optional ByVal DateToCalc As Date = Nothing) As String
If DateToCalc = Nothing Then
DateToCalc = Today
End If
Return DateToCalc
End Function






Re: Setting Today's Date by Default in a Function

TaDa

That doesn't compile.



Re: Setting Today's Date by Default in a Function

PEng1

TaDa wrote:
That doesn't compile.

Compiled and ran fine for me. What kind of error is it giving you






Re: Setting Today's Date by Default in a Function

KevinBB

Date is a value type, not a reference type. That affects comparisons to Nothing. Here are two other possibilities.

1) Compare the argument to Date.MinValue instead of comparing to Nothing. This assumes that you are interested in date and time, not just time.

Private Function DateCode1(Optional ByVal DateToCalc As Date = Nothing) As String

If DateToCalc = Date.MinValue Then

Return Date.Now.ToString

End If

Return DateToCalc.ToString

End Function

2) Make the argument Nullable(Of Date). Nullable is a structure, and structures cannot be used for optional arguments. But a second overloaded function allows you to call the function with no arguments.

Private Overloads Function DateCode2(ByVal DateToCalc As Nullable(Of Date)) As String

If Not DateToCalc.HasValue Then

Return Date.Now.ToString

End If

Return DateToCalc.Value.ToString

End Function

Private Overloads Function DateCode2() As String

Return DateCode2(Nothing)

End Function





Re: Setting Today's Date by Default in a Function

Loki70

First off, thank you all for your excellent replies.

I marked Peng1's post as the answer simply because it was the easiest one for me to understand.

Thanks again!