473,671 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting more than one result from a function.?

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

Nov 13 '05 #1
21 1753
rkc
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 testGetStringAr ray()
Dim s() As String
Dim i As Integer
s = getStringArray

For i = 0 To UBound(s)
Debug.Print s(i)
Next
End Sub

Nov 13 '05 #2
"Gerry Abbott" <pl****@ask.i e> wrote in message
news:VB******** *******@news.in digo.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(str FirstName As String, strLastName As String,
dteDOB As Date) As Boolean

' Do complicated processing to work out values
strFirstName="S teve"
strLastName="Sm ith"
dteDOB=DateSeri al(1965,01,01)
' Return True to say we have set the values
GetMyValues=Tru e

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=Stev e~LastName=Smit h~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


Nov 13 '05 #3
Thanks guys for the sharp response,
I had worked out the array passing, and my function is working properly this
way.

The solution you suggest using the user defined type looks like the most
elequent one for apps where the array is not appripriate. I'll give this one
a try for future reference. Need to work out which is quicker, array or
type. Also like how you suggest to set the function as a bolean type,
setting its value at the end of the function, then calling the function with
an expression. Neat.
Gerry

"Justin Hoffman" <j@b.com> wrote in message
news:da******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
"Gerry Abbott" <pl****@ask.i e> wrote in message
news:VB******** *******@news.in digo.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(str FirstName As String, strLastName As String,
dteDOB As Date) As Boolean

' Do complicated processing to work out values
strFirstName="S teve"
strLastName="Sm ith"
dteDOB=DateSeri al(1965,01,01)
' Return True to say we have set the values
GetMyValues=Tru e

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=Stev e~LastName=Smit h~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


Nov 13 '05 #4
On Thu, 07 Jul 2005 10:59:21 GMT, rkc
<rk*@rochester. yabba.dabba.do. rr.bomb> wrote:
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 testGetStringAr ray()
Dim s() As String
Dim i As Integer
s = getStringArray

For i = 0 To UBound(s)
Debug.Print s(i)
Next
End Sub


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(ParamArr ay 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
--
Drive C: Error. (A)bort (R)etry (S)mack The Darned Thing

Nov 13 '05 #5
rkc
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(ParamArr ay 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

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.


Nov 13 '05 #6
On Fri, 08 Jul 2005 02:45:12 GMT, rkc
<rk*@rochester. 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(ParamArr ay 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.
--
Drive C: Error. (A)bort (R)etry (S)mack The Darned Thing

Nov 13 '05 #7
rkc
Chuck Grimsby wrote:
On Fri, 08 Jul 2005 02:45:12 GMT, rkc
<rk*@rochester. 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(ParamArr ay 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.


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.

Nov 13 '05 #8
rkc wrote:
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.


Correct, except for the part stating that ParamArray arguments are
always passed ByVal and that it can't be used to return values to the
calling code.

Nov 13 '05 #9
rkc <rk*@rochester. yabba.dabba.do. rr.bomb> wrote:
: Chuck Grimsby wrote:
:> On Fri, 08 Jul 2005 02:45:12 GMT, rkc
:> <rk*@rochester. 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(ParamArr ay Stuff() As Variant) As Variant
: 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.

Does the ParamArray type have any useful function other than
reminding the coder what [s]he's doing? I would write such
code using an ordinary array and restricting it for the
functionality that I needed. Maybe that's because I've never
written code for a large project where it's assumed that
someone else will need to expand and maintain it.
--thelma
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5402
by: Konrad | last post by:
In the part of code: $polecenie = "SELECT osoby.Id ,osoby.Imie , osoby.Nazwisko,osoby.Tytul,osoby.Email,adrespraca.Adres AS ap, adrespraca.KodPoczt AS KodP,adrespraca.NazwaInstytucji,adrespraca.Miasto AS MiastP, adrespraca.Wojewodztwo AS wojPr,adrespraca.NrTelefonu As TelP,adresdom.Adres,adresdom.KodPoczt, adresdom.Miasto,adresdom.Wojewodztwo,adresdom.NrTelefonu,studia.Od,studia.Do,...
3
8643
by: Craig | last post by:
I currently have the following code: <input name="castStation" type="text" value="0" onChange="valueCalculate()";> The function called does a few caluclations and writes the total to a total box. However, if the user type a non-numeric char, I get NaN.
15
2226
by: John J | last post by:
I've written the following code into a class to search for and display the results of all races entered (The complete code is in a previous thread). I wish to amend the code so as to display the best result only. Can anyone suggest a simple amendment to the following that will result in only the best result being displayed?
6
2347
by: kstriyhon | last post by:
i need to get data from a drillthrough result this data is in a pivottable fieldset and i need to retrieve all the values of this fieldset, in order to put them in a string how can i do this?? any help would be appreciate, thank's in advance.
43
2064
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions within main? (It just doesn't "feel" right to me). Example 1: class MyProg { public:
6
3566
by: Yang | last post by:
I have a page with source code like this: <script type="text/javascript"> function displayResult(data) { } } </script> <script type="text/javascript" src="http://something.com/data?
0
1552
oll3i
by: oll3i | last post by:
import javax.swing.*; import java.awt.event.*; import java.util.ArrayList; import java.util.List; import java.util.concurrent.*; import java.lang.reflect.*; public class Exec1 extends JFrame implements ActionListener { int k = 0;
1
2801
by: Gary Robinson | last post by:
Alex Martelli has a cookbook recipe, whoami, for retrieving the name of the current function: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062. It uses sys._getframe(). I'm a little wary about using sys._getframe() because of the underscore prefix and the fact that the python docs say "This function should be used for internal and specialized purposes only." I feel more comfortable using the equivalent functionality in the...
2
1310
by: pargat.singh | last post by:
Hi Everyone: Please help as i need to write some rounding function in C#. i Wrote one but does not give me correct result. Can some one please correct me C# Codes ######################################################################### dblVal = 1234567.89
2
1759
by: Andrew Cooper | last post by:
Greetings, I'm creating a website using ASP.NET. In creating my DAL I've got a Table Adapter that I've set up to use an existing Stored Procedure from an SQL Server 2000 database. However, when I select the stored procedure I want to use the fields that are returned do not show up in the listbox for that stored procedure. If I continue and attempt to finish the Table Adapter I get the following error (even though the Adapter is...
0
8481
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8400
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8602
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8672
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.