从函数中获取多个结果。? [英] Getting more than one result from a function.?

查看:74
本文介绍了从函数中获取多个结果。?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如果完成了一个将字符串解析为片段(字段)的脚本,那么

会将这些子字符串分配到一个数组中。


我希望把它变成一个我可以传递字符串的函数。然而

i不知道如何获取数组的内容?

我习惯将参数传递给函数,并获得单个结果

回来。有没有办法从函数中获得多个结果?

是否与传递byVal或byRef有关?

提前感谢,

Gerry Abbott

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

推荐答案

Gerry Abbott写道:
Gerry Abbott wrote:
大家好,如果完成了一个将字符串解析为片段(字段)的脚本,那么
将这些子字符串分配到一个数组中。

我希望把它变成一个我可以传递字符串的函数。但是我不知道如何获取阵列的内容?
我习惯将参数传递给函数,并获得单个结果
。有没有办法从函数中获取多个结果?
是否与传递byVal或byRef有关?
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?




你可以通过传入来实现字符串数组或变量byref

并且方法将值放入数组中。

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)=粉红色


s = a

结束子


Sub testGetByRef()

Dim s()As String

Dim i As Integer

致电getByRef(s)


For i = 0到UBound(s)

Debug.Print s(i)

下一页

结束子


你也可以只返回一个数组作为函数的结果。


函数getStringArray()As String()

Dim a(5 )作为Str ing

a(0)="黄色"

a(1)=" orange"

a(2)=" blue"

a(3)=" lemon"

a(4)=" tangerine"

a(5)=" pink"


getStringArray = a

结束函数


Sub testGetStringArray()

Dim s()作为String

Dim i As Integer

s = getStringArray


for i = 0 To UBound(s)

Debug.Print s(i)

下一页

End Sub



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


" Gerry Abbott < PL **** @ ask.ie>在消息中写道

新闻:VB *************** @ news.indigo.ie ...
"Gerry Abbott" <pl****@ask.ie> wrote in message
news:VB***************@news.indigo.ie...
大家好,<如果完成了一个将字符串解析为片段(字段)的脚本,那么
将这些子字符串分配到一个数组中。

我希望将其转换为我可以通过的函数字符串。
但是我不知道如何获取数组的内容?
我习惯将参数传递给函数,并获得单个结果
。有没有办法从函数中获取多个结果?
是否与传递byVal或byRef有关?

提前感谢,

Gerry Abbott
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




最近有关于ByVal / ByRef的讨论,关于函数

参数通常如何通过ByRef传递(除非你指定ByVal)。这意味着

该函数可以改变那些实际值,你可以使用它作为一种从函数中获取多个值的方式 - 也就是说,你会得到一个

结果是说所有值都已加载到你传递的参数中:


公共函数GetMyValues(strFirstName As String,strLastName As String,

dteDOB As Date)As Boolean


''进行复杂处理以计算出值

strFirstName =" Steve"

strLastName =" Smith"

dteDOB = DateSerial(1965,01,01)

''返回True到说我们设定了价值

GetMyValues =真


结束功能


但这不是我的方法经常看到使用过。你可以得到

函数来返回一个字符串,如:

" FirstName = Steve~LastName = Smith~DOB = 1965-01-01"然后将其拆分。或者

您可以获得返回数组的功能。


或者用户定义的数据类型如何,例如


类型人物

FirstName As String

LastName As String

DOB As Date

结束类型


公共函数LoadPerson(psn As Person)As Boolean


psn .LastName =" Smith"

psn.DOB = DateSerial(1965,1,1)


LoadPerson = True


结束功能


Public Sub TestPerson()


Dim psn As person


如果LoadPerson(psn)那么

MsgBox" hello I''m" &安培; psn.FirstName& " " &安培; psn.LastName

结束如果


结束子




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



谢谢大家的敏锐回应,

我已经计算出阵列传递,我的功能正常运行这个

方式。


您建议使用用户定义类型的解决方案对于数组不适合的应用程序看起来是最好的
elequent。我会给这一个

尝试以备将来参考。需要解决哪个更快,阵列或

类型。同样你建议将函数设置为bolean类型,

在函数末尾设置它的值,然后用

表达式调用函数。整洁。

格里


" Justin Hoffman" < j@b.com>在消息中写道

news:da ********** @ nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com ...
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.ie>在消息中写道
新闻:VB *************** @ news.indigo.ie ...
"Gerry Abbott" <pl****@ask.ie> wrote in message
news:VB***************@news.indigo.ie...
大家好,
如果完成了一个脚本,将一个字符串解析成片段(字段),然后将这些子字符串分配到一个数组中。

我希望把它变成一个我可以传递字符串的函数。但是我不知道如何获取数组的内容?
我习惯将参数传递给函数,然后得到一个
结果。有没有办法从函数中获取多个结果?
是否与传递byVal或byRef有关?

提前感谢,

Gerry Abbott
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



最近有关于ByVal / ByRef的讨论,关于函数
参数通常如何通过ByRef传递(除非你指定ByVal)。这意味着该函数可以改变那些实际值,你可以使用
这个方法从函数中获取多个值 - 也就是说,你会得到一个结果来说明是否所有值都已加载到您传递的
参数中:

Public Function GetMyValues(strFirstName As String,strLastName As String,
dteDOB As Date)As Boolean

''进行复杂的处理以计算出值
strFirstName =" Steve"
strLastName =" Smith"
dteDOB = DateSerial(1965,01,01)''返回True表示我们设置了值
GetMyValues = True

结束功能

但这不是我经常使用的方法。你可以得到
函数返回一个字符串,如:
" FirstName = Steve~LastName = Smith~DOB = 1965-01-01"然后将其拆分。或者
你可以获得返回数组的功能。

或者用户定义的数据类型如何,例如

类型人物
FirstName As String
LastName As String
DOB作为日期
结束类型

公共函数LoadPerson(psn As Person)As Boolean

psn.FirstName =" Steve"
psn.LastName =" Smith"
psn.DOB = DateSerial(1965,1,1)

LoadPerson = True

Public Sub TestPerson()

Dim psn As Person

如果LoadPerson(psn)那么
MsgBox"你好,我是&安培; psn.FirstName& " " &安培; psn.LastName
结束如果

结束子



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



这篇关于从函数中获取多个结果。?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆