Results 1 to 7 of 7

Thread: Sending a variable number of arguments to a sub

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Sending a variable number of arguments to a sub

    I am working on an inventory ordering system where I send a different number of email addresses depending on the vendor. I am trying to use one sub to do this instead of the 8 subs it would take if I created one for each vendor.

    I have 1 sub that requires 6 Send addresses, 3 that require 4 Send Addresses and 4 that require 4 Send Addresses. Is it possible to this all with one sub leaving blanks in the arguments so that they are ignored?

    Code:
        Private Sub FireEMail()
            ' The first email has 6 addresses
    
            MightyEmail( _
                "[email protected]", _
                "[email protected]", _
                "[email protected]", _
                "[email protected]", _
                "[email protected]", _
                "[email protected]", _
                "[email protected]", _
                "strSubject", "strBody", "strPath")
    
            ' The second email has 4 addresses
    
            MightyEmail( _
                "[email protected]", _
                "strTo1Somewhereelse.com", _
                "StrTo2Somewhereelse.com", _
                "StrTo3Somewhereelse.com", _
                "strSubject", "strBody", "strPath")
            ' The fourth has 4 etc..
    
        End Sub
    
        Private Sub MightyEmail( _
            ByVal StrMailFrom As String, _
            ByVal strMailTo1 As String, _
            ByVal strMailTo2 As String, _
            ByVal strMailTo3 As String, _
            ByVal strMailTo4 As String, _
            ByVal strMailTo5 As String, _
            ByVal strmailTo6 As String, _
            ByVal StrSubject As String, _
            ByVal StrBody As String, _
            ByVal strFPath As String)
    
            Dim message As New MailMessage
            message.From = New MailAddress(StrMailFrom)
            message.To.Add(strMailTo1)
            message.To.Add(strMailTo2)
            message.To.Add(strMailTo3)
            message.To.Add(strMailTo4)
            message.To.Add(strMailTo5)
            message.To.Add(strmailTo6)
            message.Subject = StrSubject
            message.Body = StrBody
    
            Dim emailClient As New SmtpClient(strMailServer)
            emailClient.Credentials = (SMTP_Cred)
            emailClient.DeliveryMethod = SmtpDeliveryMethod.Network
            emailClient.EnableSsl = True
            Try
                emailClient.Send(message)
                MessageBox.Show("done")
            Catch ex As Exception
                MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace)
            Finally
    
            End Try
        End Sub
    Last edited by FastEddie; Apr 17th, 2007 at 08:42 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Sending a variable number of arguments to a sub

    I would suggest overloading the method, e.g.
    vb Code:
    1. Public Overloads Sub MyMethod(ByVal str1 As String)
    2.     MyMethod(str1, Nothing, Nothing)
    3. End Sub
    4.  
    5. Public Overloads Sub MyMethod(ByVal str1 As String, ByVal str2 As String)
    6.     MyMethod(str1, str2, Nothing)
    7. End Sub
    8.  
    9. Public Overloads Sub MyMethod(ByVal str1 As String, ByVal str2 As String, ByVal str3 As String)
    10.     'Do whatever with the arguments here.
    11. End Sub
    Some might suggest using optional arguments but I'd recommend against that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Sending a variable number of arguments to a sub

    I suggest passing an array of email addresses so you don't need n overloads.

    Code:
    Public Sub MightyEmail(Addresses As String())

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Sending a variable number of arguments to a sub

    The problem with the array is that it can be any length. By using an overloaded method you ensure that only the specific numbers of arguments that you want can ever be used. The array will certainly work, but if you were going to do it that way then I'd use a paramarray:
    vb Code:
    1. Public Sub MightyEmail(ByVal ParamArray Addresses As String())
    That way you can choose to call it by passing a single string array or multiple strings.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Sending a variable number of arguments to a sub

    The overloads worked perfect, once I figured out how to apply it correctly. Thank you!

  6. #6
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    Re: Sending a variable number of arguments to a sub (Resolved)

    Just to add a third method, ParamArray

    vb Code:
    1. Public Sub DoSomething(ByVal ParamArray batchOfStuff() As String)

    I kind of like this method when I might have no parameters, a single item or few items being sent to a routine. One example would be sort criteria.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Sending a variable number of arguments to a sub

    Oops I didn't test it as well as I should have before I marked it resolved. I must have done something wrong. I used the Public Overrides on my email Sub like this.
    Code:
        Public Overloads Sub MightyEmail( _
            ByVal strFPath As String, _
            ByVal StrMailFrom As String, _
            ByVal StrSubject As String, _
            ByVal StrBody As String, _
            ByVal strMailTo1 As String, _
            ByVal strMailTo2 As String, _
            ByVal strMailTo3 As String, _
            ByVal strMailTo4 As String, _
            ByVal strMailTo5 As String, _
            ByVal strmailTo6 As String)
     ....
    ' My agrument was sent like this.
            MightyEmail( _
                "\\Server\Company\Orcer\D5\", _
                "[email protected]", _
                "TEST Order for D5", _
                "TEST", 
                "[email protected]", _
                "[email protected]", _
                Nothing, _
                Nothing, _
                Nothing, _
                Nothing)
    I recieved this error with the third MailTo highlighted :

    Message="Value cannot be null.
    Parameter name: addresses"
    ParamName="addresses"
    Source="System"

    What did I do wrong?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width