Default values for args

D

Dragon

Hi,

Is there a way to set defaults values for args of a function/sub?

For example:

Function:
public function testFunc (name as string, age as integer = 30, phone as
string)

Calling the Function:
testFunc("John", 25, "555-1212")
testFunc("John", , "555-1212") ' Age will be set to default of 30

This way one or more of arg can be left out and still have a working
function/sub.

Thank you.
 
B

Brx

Your so close, it's Public Function testFunc(name as string, phone as
string, optional age as integer = 30)
Optional parameters must be last in the function, so anything after age must
also be optional.

kmurphy
roughly at nexusinfosys in the com domain.
 
H

Herfried K. Wagner [MVP]

Dragon said:
Is there a way to set defaults values for args of a function/sub?

For example:

Function:
public function testFunc (name as string, age as integer = 30, phone as
string)

Calling the Function:
testFunc("John", 25, "555-1212")
testFunc("John", , "555-1212") ' Age will be set to default of 30

This way one or more of arg can be left out and still have a working
function/sub.

You can archieve this behavior with optional parameters:

\\\
Public Sub TestFunc( _
ByVal Name As String, _
Optional ByVal Age As Integer = 30, _
Optional ByVal ID As String = Nothing _
)
...
End Sub
///
 
D

Dragon

Thank you :)

Brx said:
Your so close, it's Public Function testFunc(name as string, phone as
string, optional age as integer = 30)
Optional parameters must be last in the function, so anything after age
must also be optional.

kmurphy
roughly at nexusinfosys in the com domain.
 
D

Dragon

Is it not available in C#?


Bob Powell said:
<sigh> I really miss this in C#...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
H

Herfried K. Wagner [MVP]

Dragon said:
Is it not available in C#?

No, it isn't. However, code written in C# can consume/call methods which
have optional parameters.
 
D

Dragon

Hi,

You can have optional arguments, but they must be the last ones in the
list of arguments, e.g.:

~
Public Function TestFunc (ByVal Name As String, ByVal Phone As
String, Optional ByVal Age As Integer = 30) As BadImageFormatException
~

I would recommend you to have multiple overloaded versions of function
i.e.:

~
Public Function TestFunc(ByVal Name As String, ByVal Phone As
String) As BadImageFormatException
Return TestFunc(Name, Phone, 30)
End Function

Public Function TestFunc(ByVal Name As String, ByVal Phone As
String, ByVal Age As Integer) As BadImageFormatException
REM Your code goes here...
End Function
~

HTH,
Roman
 
M

Mythran

Dragon said:
Hi,

Is there a way to set defaults values for args of a function/sub?

For example:

Function:
public function testFunc (name as string, age as integer = 30, phone as
string)

Calling the Function:
testFunc("John", 25, "555-1212")
testFunc("John", , "555-1212") ' Age will be set to default of 30

This way one or more of arg can be left out and still have a working
function/sub.

Thank you.

I don't know why I don't like optional parameters...I think it was because
some other languages don't see those methods when accessing them via COM?
Anywho, a way around it would be:

Public Overloads Function TestFunc(ByVal Name AS String, ByVal Age As
Integer, ByVal Phone As String) As Whatever

End Function

Public Overloads Function TestFunc(ByVal Name As String, ByVal Phone As
String) As Whatever
TestFunc(Name, 30, Phone)
End Function

Public Overloads Function TestFunc(ByVal Name As String)
TestFunc(Name, 30, "555-1212")
End Function

Calls like:

TestFunc("John", 30, "555-1212")
TestFunc("John", "555-1212")
TestFunc("John")

Use overloading to achieve what you want, similarly....

HTH,
Mythran
 
D

Dragon

Thank you Mythran.



Mythran said:
I don't know why I don't like optional parameters...I think it was because
some other languages don't see those methods when accessing them via COM?
Anywho, a way around it would be:

Public Overloads Function TestFunc(ByVal Name AS String, ByVal Age As
Integer, ByVal Phone As String) As Whatever

End Function

Public Overloads Function TestFunc(ByVal Name As String, ByVal Phone As
String) As Whatever
TestFunc(Name, 30, Phone)
End Function

Public Overloads Function TestFunc(ByVal Name As String)
TestFunc(Name, 30, "555-1212")
End Function

Calls like:

TestFunc("John", 30, "555-1212")
TestFunc("John", "555-1212")
TestFunc("John")

Use overloading to achieve what you want, similarly....

HTH,
Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top