返回2个值 [英] Returning 2 values

查看:53
本文介绍了返回2个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我知道这是基本的,但它是我从未学过的东西!


我有一个向Sub返回True或False的函数。但实际上我想做的是b $ b返回True或False以及一个数字。这怎么可能?
怎么可能?


谢谢


-

我' B请Bob。

解决方案

Bob Hollness写道:

我有一个函数返回一个True或者对Sub来说是假的。但我实际想要做的是返回True或False以及一个数字。这怎么可能?




有(至少)两种方法可以做到这一点。


通常最简单的方法是返回其中一个值作为

过程的结果,并允许用户提供一个变量作为参数,

将接收另一个值。例如,你可以将你的功能设置为

如下:

公共函数DoSomething(ByRef ReturnValue As Integer)As Boolean

ReturnValue = 10

返回真实

结束功能


当你调用它时,你提供的任何变量作为ReturnValue

参数将填充值10(因为变量是

通过引用传递而不是按值传递)。布尔返回值的作用是

正常。


另一种可以做到这一点的方法是返回一个结构或类

包含两个值。这在某些情况下很有用,但是对于像你看起来那样简单的事情,ByRef变量可能是实现和使用的最简单的解决方案。


希望有所帮助,


-


(O)enone


使用F#而不是VB并使用元组。


或创建一个元组类并使用它来封装布尔和整数

字段。从你的函数返回这个元组。


公共类元组

private _myBoolean as boolean

private _myNumber as integer

公共财产MyBoolean

get

return _myBoolean

end get

set(byval value as boolean )

_myBoolean = value

结束集

结束财产

公共财产MyNumber

get

return _myNumber

end get

set(byval value as integer)

_myNumber = value

结束集

结束财产

终结类


" Bob Hollness" < bo*@blockbuster.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...

大家好。我知道这是基本的,但它是我从未学过的东西!

我有一个函数,它返回Sub的真或假。但我实际想要做的是返回True或False以及一个数字。这怎么可能?

谢谢

-
我会有一个B请Bob。



听起来不错,但我认为在Return完成后,VB离开

函数并返回到调用的那个,因此没有达到第二个

返回。


??


" Oenone" <无*** @ nowhere.com>在消息中写道

news:uZ ************** @ TK2MSFTNGP12.phx.gbl ...

Bob Hollness写道:< blockquote class =post_quotes>我有一个向Sub返回True或False的函数。但我实际想要做的是返回True或False以及一个数字。这怎么可能?



有(至少)两种方法可以做到这一点。

通常最简单的方法是返回其中一个作为程序结果的值,并允许用户提供一个变量作为参数
,它将接收另一个值。例如,您可以按如下方式设置
函数:

公共函数DoSomething(ByRef ReturnValue As Integer)As Boolean
ReturnValue = 10
返回True
当你调用它时,你提供的任何变量作为ReturnValue
参数将填充值10(因为变量是通过引用传递而不是按价值)。布尔返回值正常工作


另一种方法是返回一个包含两个值的结构或类。这在某些情况下很有用,但是对于像你看起来那样简单的事情,ByRef变量可能是最容易实现和使用的解决方案。

希望有所帮助,


(O)enone



Hi all. I know this is basic but it is something that i have never learnt!

I have a function that returns a True or False to the Sub. But what i
actually want to do is return True or False and a number. How is this
possible?

Thanks

--
I''ll have a B please Bob.

解决方案

Bob Hollness wrote:

I have a function that returns a True or False to the Sub. But what i
actually want to do is return True or False and a number. How is this
possible?



There are (at least) two ways to do this.

Usually the simplest way is to return one of the values as the result of the
procedure, and allow the user to provide a variable as a parameter which
will receive the other value. For example, you could set your function up as
follows:

Public Function DoSomething(ByRef ReturnValue As Integer) As Boolean
ReturnValue = 10
Return True
End Function

When you call this, whichever variable you supply as the ReturnValue
parameter will be populated with the value 10 (because the variable was
passed By Reference instead of By Value). The boolean return value works as
normal.

Another way you could do this is by returning a structure or a class which
contains the two values. This is useful in some circumstances but for
something as simple as you appear to be doing, a ByRef variable is probably
the easiest solution to implement and use.

Hope that helps,

--

(O)enone


Use F# instead of VB and use a tuple.

Or create a Tuple Class and use that to encapsulate a boolean and integer
fields. Return this Tuple from your function.

public class Tuple
private _myBoolean as boolean
private _myNumber as integer
public property MyBoolean
get
return _myBoolean
end get
set(byval value as boolean)
_myBoolean = value
end set
end property
public property MyNumber
get
return _myNumber
end get
set(byval value as integer)
_myNumber = value
end set
end property
end class

"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

Hi all. I know this is basic but it is something that i have never
learnt!

I have a function that returns a True or False to the Sub. But what i
actually want to do is return True or False and a number. How is this
possible?

Thanks

--
I''ll have a B please Bob.



sounds good but i thought that after the Return had been actioned, VB left
the function and went back to the calling one, thus not reaching the 2nd
Return.

??

"Oenone" <no***@nowhere.com> wrote in message
news:uZ**************@TK2MSFTNGP12.phx.gbl...

Bob Hollness wrote:

I have a function that returns a True or False to the Sub. But what i
actually want to do is return True or False and a number. How is this
possible?



There are (at least) two ways to do this.

Usually the simplest way is to return one of the values as the result of
the procedure, and allow the user to provide a variable as a parameter
which will receive the other value. For example, you could set your
function up as follows:

Public Function DoSomething(ByRef ReturnValue As Integer) As Boolean
ReturnValue = 10
Return True
End Function

When you call this, whichever variable you supply as the ReturnValue
parameter will be populated with the value 10 (because the variable was
passed By Reference instead of By Value). The boolean return value works
as normal.

Another way you could do this is by returning a structure or a class which
contains the two values. This is useful in some circumstances but for
something as simple as you appear to be doing, a ByRef variable is
probably the easiest solution to implement and use.

Hope that helps,

--

(O)enone



这篇关于返回2个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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