Solved

assigning default value if not passed to public sub

Posted on 2004-10-24
201 Views
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

thx
Johnny
aka Pern
0
Question by:Johnny
    4 Comments
     
    LVL 20

    Accepted Solution

    by:
    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
     
    LVL 85

    Expert Comment

    by: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
    0
     
    LVL 20

    Expert Comment

    by: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)
    0
     

    Author Comment

    by:Johnny
    Thanks thats what i was looking for
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now