Sign up to the jQuery Grid Subscription list.

Return multiple values from a function (Visual Basic General)

Original post by c.hunter at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2415779&SiteID=1


The Question :
I have a function that sends out emails to four users. I am to add another a fifth one but i can get it to go out can some one please help? Here is my code...

Thanks





Private Function GetRecipient(ByVal state As String) As String

Dim Central As String = "AR,IA,IL,KS,LA,MN,MO,MS,NE,OK,TN,TX,WI"

Dim Eastern As String = "AL,CT,DC,DE,FL,GA,IN,KY,MA,MD,ME,MI,NC,NH,NJ,NY,OH,PA,RI,SC,VA,VT,WV"

Dim Western As String = "AK,AZ,CA,CO,HI,ID,MT,ND,NM,NV,OR,SD,UT,WA,WY"

If state.Split(" - ").Length >= 1 Then

Dim shortState As String = state.Split(" - ")(state.Split(" - ").Length - 1)

'lblerror.Text = shortState

If Central.IndexOf(shortState) >= 0 Then

Return "cxcas@domain.com"

ElseIf Eastern.IndexOf(shortState) >= 0 Then

Return "fndda@domain.com"

ElseIf Western.IndexOf(shortState) >= 0 Then

Return "assddf@domain.com" <------------------------------- Want to add the other email address here. ElseIf shortState = "USA" Then Return "kortaed@domain.com" Else Return "NONE" End If Else Return "NONE" End If End Function
The Accepted answer :

Create a StringArray and store all return values into it. Then return the Array.
Ex:

Function ReturnArrayExample() as String()
Dim strarray(2) as String
strarray(0)= value 1
strarray(1)= value 2
strarray(2)= value 3

return strarray

End Function

To access values from Array use strarray(indexnumber).
Ex:
msgbox(strarray(0))

This will display value 1.

So your Function is modified like this:


Private Function GetRecipient(ByVal state As String) As String()

Dim strarray(3) as String


Dim Central As String = "AR,IA,IL,KS,LA,MN,MO,MS,NE,OK,TN,TX,WI"

Dim Eastern As String = "AL,CT,DC,DE,FL,GA,IN,KY,MA,MD,ME,MI,NC,NH,NJ,NY,OH,PA,RI,SC,VA,VT,WV"

Dim Western As String = "AK,AZ,CA,CO,HI,ID,MT,ND,NM,NV,OR,SD,UT,WA,WY"

If state.Split(" - ").Length >= 1 Then

Dim shortState As String = state.Split(" - ")(state.Split(" - ").Length - 1)

'lblerror.Text = shortState

If Central.IndexOf(shortState) >= 0 Then

strarray(0)="cxcas@domain.com"

ElseIf Eastern.IndexOf(shortState) >= 0 Then

strarray(0)="fndda@domain.com"

ElseIf Western.IndexOf(shortState) >= 0 Then

strarray(0)="assddf@domain.com" <------------------------------- Want to add the other email address here. strarray(1)="another email address" ElseIf shortState = "USA" Then strarray(0)="kortaed@domain.com" Else strarray(2)= "NONE" End If Else strarray(3)= "NONE" End If return strarray End Function I hope this will help. More Accepted solutions :

Hi,



Here is another way to do it, I have changed the FUNCTION to a SUB and still changed some array values.



Add one button to a FORM to test this code please.









Regards,



John

______________________________________________







Option Strict Off



Public Class Form1



'Declare this array big enough for your needs here.>>

Private email(9) As String



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Call the SUB Test() to test the FUNCTION.

Call Test()

End Sub









Private Sub Test()



Call GetRecipient("CA")



Dim outputString As String = ""

For index As Integer = 0 To 9

If email(index) <> "" Then

outputString &= email(index) & ControlChars.NewLine

End If

Next

MessageBox.Show(outputString)

End Sub









Private Sub GetRecipient(ByVal state As String)

Dim Central As String = "AR,IA,IL,KS,LA,MN,MO,MS,NE,OK,TN,TX,WI"

Dim Eastern As String = "AL,CT,DC,DE,FL,GA,IN,KY,MA,MD,ME,MI,NC,NH,NJ,NY,OH,PA,RI,SC,VA,VT,WV"

Dim Western As String = "AK,AZ,CA,CO,HI,ID,MT,ND,NM,NV,OR,SD,UT,WA,WY"

If state.Split(" - ").Length >= 1 Then

Dim shortState As String = state.Split(" - ")(state.Split(" - ").Length - 1)

'lblerror.Text = shortState

If Central.IndexOf(shortState) >= 0 Then

email(0) = "cxcas@domain.com"



ElseIf Eastern.IndexOf(shortState) >= 0 Then

email(0) = "cxcas@domain.com"



ElseIf Western.IndexOf(shortState) >= 0 Then

'Now changing two values in the array named "email".

'No RETURN statements are needed as this is now a SUB.

email(0) = "cxcas@domain.com"

email(1) = "joe-bloggs@domain.com"



ElseIf shortState = "USA" Then

email(0) = "kortaed@domain.com"

Else

End If

Else

End If

End Sub

End Class

0 comments:

Related Ads