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
JohnnyAsked:
Who is Participating?
 
ihenryCommented:
You can check if the parameter is passed as null

Public Sub SendEmail(ByVal Mail_To As String)

     If IsNothing( Mail_To ) Then
        Mail_To  = "pern@dragonsworkshop.com"
     End If

     oMsg.To = Mail_To
     .......
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
ihenryCommented:
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)
0
 
JohnnyAuthor Commented:
Thanks thats what i was looking for
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.