|
-
Apr 16th, 2007, 11:13 PM
#1
Thread Starter
Hyperactive Member
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.
-
Apr 16th, 2007, 11:30 PM
#2
Re: Sending a variable number of arguments to a sub
I would suggest overloading the method, e.g.
vb Code:
Public Overloads Sub MyMethod(ByVal str1 As String)
MyMethod(str1, Nothing, Nothing)
End Sub
Public Overloads Sub MyMethod(ByVal str1 As String, ByVal str2 As String)
MyMethod(str1, str2, Nothing)
End Sub
Public Overloads Sub MyMethod(ByVal str1 As String, ByVal str2 As String, ByVal str3 As String)
'Do whatever with the arguments here.
End Sub
Some might suggest using optional arguments but I'd recommend against that.
-
Apr 17th, 2007, 12:33 AM
#3
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())
-
Apr 17th, 2007, 12:44 AM
#4
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:
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.
-
Apr 17th, 2007, 07:36 AM
#5
Thread Starter
Hyperactive Member
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!
-
Apr 17th, 2007, 08:10 AM
#6
Re: Sending a variable number of arguments to a sub (Resolved)
Just to add a third method, ParamArray
vb Code:
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.
-
Apr 17th, 2007, 08:51 AM
#7
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|