Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

assigning default value if not passed to public sub

i have a sub that sends an email i want to know how to assing a value to it if its not pased ie


Public Sub SendEmail(ByVal Mail_To As String)

oMsg.To = Mail_To  ' ' if Mail_To is null/empty then use pern@dragonsworkshop.com

.......


the suib works fine if i pass it but i want to not HAVE to pass a few values

thx
Johnny
aka Pern
ASKER CERTIFIED SOLUTION
Avatar of ihenry
ihenry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
You can declare the parameter as Optional and give it a default value as follows:

    Public Sub SendEmail(Optional ByVal Mail_To As String = "pern@dragonsworkshop.com")

    End Sub

Any parameters that follow Mail_To must also be declared as Optional as well.

~IM
Avatar of ihenry
ihenry

Or you can force the function to take only non null parameter value as the following

Public Sub SendEmail(ByVal Mail_To As String)

     If IsNothing( Mail_To ) Then
        Throw New ArgumentNullException( "Mail_To" )
     End If

     oMsg.To = Mail_To
     .......

And overload the function, like so

Public Sub SendEmail()
     SendEmail( "pern@dragonsworkshop.com" )
End Sub

This is C# style :o)
Avatar of Johnny

ASKER

Thanks thats what i was looking for