![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string. However i don't know how to get the contents of the array out? I am used to passing parameters to a function, and getting a single result back. Is there a way to get multiple results from a function? Is it something to do with passing byVal or byRef? |
#3
| |||
| |||
|
|
Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string. However i don't know how to get the contents of the array out? I am used to passing parameters to a function, and getting a single result back. Is there a way to get multiple results from a function? Is it something to do with passing byVal or byRef? Thanks in advance, Gerry Abbott |
#4
| |||
| |||
|
|
"Gerry Abbott" <please (AT) ask (DOT) ie> wrote in message news:VB7ze.2134$R5.526 (AT) news (DOT) indigo.ie... Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string. However i don't know how to get the contents of the array out? I am used to passing parameters to a function, and getting a single result back. Is there a way to get multiple results from a function? Is it something to do with passing byVal or byRef? Thanks in advance, Gerry Abbott There was a recent discussion on ByVal/ByRef concerning how function arguments are normally passed ByRef (unless you specify ByVal). This means that the function can change those actual values and you could use this as a way to get multiple values from a function - that is, you would get a single result to say whether all the values had been loaded into the parameters you passed: Public Function GetMyValues(strFirstName As String, strLastName As String, dteDOB As Date) As Boolean ' Do complicated processing to work out values strFirstName="Steve" strLastName="Smith" dteDOB=DateSerial(1965,01,01) ' Return True to say we have set the values GetMyValues=True End Function But this is not a method I have seen used very often. You could get you function to return a string like: "FirstName=Steve~LastName=Smith~DOB=1965-01-01" and split it up later. Or you could get the function to return an array. Or how about a user-defined data type, eg Type Person FirstName As String LastName As String DOB As Date End Type Public Function LoadPerson(psn As Person) As Boolean psn.FirstName = "Steve" psn.LastName = "Smith" psn.DOB = DateSerial(1965, 1, 1) LoadPerson = True End Function Public Sub TestPerson() Dim psn As Person If LoadPerson(psn) Then MsgBox "hello I'm " & psn.FirstName & " " & psn.LastName End If End Sub |
#5
| |||
| |||
|
|
Gerry Abbott wrote: Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string. However i don't know how to get the contents of the array out? I am used to passing parameters to a function, and getting a single result back. Is there a way to get multiple results from a function? Is it something to do with passing byVal or byRef? You can do it by passing in a string array or a variant byref and having the method put values in the array. Sub getByRef(ByRef s() As String) Dim a(5) As String a(0) = "yellow" a(1) = "orange" a(2) = "blue" a(3) = "lemon" a(4) = "tangerine" a(5) = "pink" s = a End Sub Sub testGetByRef() Dim s() As String Dim i As Integer Call getByRef(s) For i = 0 To UBound(s) Debug.Print s(i) Next End Sub You can also just return an array as the result of the function. Function getStringArray() As String() Dim a(5) As String a(0) = "yellow" a(1) = "orange" a(2) = "blue" a(3) = "lemon" a(4) = "tangerine" a(5) = "pink" getStringArray = a End Function Sub testGetStringArray() Dim s() As String Dim i As Integer s = getStringArray For i = 0 To UBound(s) Debug.Print s(i) Next End Sub |
#6
| |||
| |||
|
|
You can also pass a bunch of variables to a function as a ParamArray: (This example was actually written by Joe Foster, of "Nuke Me Zemu" legend) Function RowAvg(ParamArray Stuff() As Variant) As Variant Dim c As Integer: c = 0 Dim s As Variant Dim i As Integer For i = LBound(Stuff) To UBound(Stuff) If IsMissing(Stuff(i)) Then ' skip it ElseIf IsNull(Stuff(i)) Then ' skip it too Else s = s + Stuff(i) c = c + 1 End If Next If c > 0 Then RowAvg = s / c Else RowAvg = Null End Function |
#7
| |||
| |||
|
|
Chuck Grimsby wrote: You can also pass a bunch of variables to a function as a ParamArray: (This example was actually written by Joe Foster, of "Nuke Me Zemu" legend) Function RowAvg(ParamArray Stuff() As Variant) As Variant That's nice, but using an array of a defined type would clear up what the function actually expected to be passed in. It's tough to guarantee a result when your only argument will except any number of any data type, including objects. |
#8
| |||
| |||
|
|
On Fri, 08 Jul 2005 02:45:12 GMT, rkc rkc (AT) rochester (DOT) yabba.dabba.do.rr.bomb> wrote: Chuck Grimsby wrote: You can also pass a bunch of variables to a function as a ParamArray: (This example was actually written by Joe Foster, of "Nuke Me Zemu" legend) Function RowAvg(ParamArray Stuff() As Variant) As Variant That's nice, but using an array of a defined type would clear up what the function actually expected to be passed in. It's tough to guarantee a result when your only argument will except any number of any data type, including objects. Personally, I quite agree! Indeed, I tend to write code in exactly that fashion myself, but I also think it's important to know _all_ the options, not just those I happen to agree with. |
#9
| |||
| |||
|
|
You're right. So let's add that a paramarray is always optional, must always be the last argument in the list and is always passed ByVal. Always being passed ByVal means it can't be used as a way to return values to the calling code. |
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |