Default function parameters in VB.NET

burak

Centurion
Joined
Jun 17, 2003
Messages
127
Hello,

In C/C++ you can have functions with default values.

From http://www.uq.net.au/~zzmiadam/tute/Basic/functions.htm

" char DefaultFunc( int A = 5 )

means that if the function is called without any parameters passed, the value for A defaults to 5, otherwise it takes on the value passed, so:

result = DefaultFunc(22) ; //A is assigned a value of 22
result = DefaultFunc() ; //value of A defaults to 5

"

Can you do the same in VB.NET?

I have a function that returns the next business day

"Function ReturnNextBusinessDay(ByVal inDate As Date) As Date"

I want the function declaration to be in such a way that inDate will have a blank default value. The function will use today's date if inDate is blank.

How can this be done?

Thank you,

Burak
 
You can use the Optional keyword, but this is only supported in VB.NET and not in any other language. The right way to do this is to make overloads.
 
Thanks,

I wrote a declaration like this

Function ReturnNextBusinessDay(Optional ByVal inDate
As Date = #1/1/1000#) As Date

I just check for 1/1/1000 in the function and use
today's date if function was called with no
parameters.

I would like to compile this function and make it
available to other pages in the project.

- What kind of file should I place this function in ?

ie.. .aspx, etc...

- How do I call this function from other pages? From
the code behind files especially.

Thanks,

Burak
 
If you want all pages to have access to it then put in a class in your project, and make the function shared if you dont want to create objects of the class.
 
Back
Top