[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


assigning default value if not passed to public sub

Posted on 2004-10-24
Medium Priority
Last Modified: 2010-04-24
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

aka Pern
Question by:Johnny
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 20

Accepted Solution

ihenry earned 2000 total points
ID: 12393583
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
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12393610
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.

LVL 20

Expert Comment

ID: 12393623
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)

Author Comment

ID: 12394184
Thanks thats what i was looking for

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question