byVal Vs.按地址 [英] byVal Vs. byRef

查看:68
本文介绍了byVal Vs.按地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


好​​的这里是senerio:


.....


Dim myArrayList as New ArrayList(0)

me.Test_A(myArrayList)


myArralist.Count> 0''这将是正确的。


Public Sub Test_A(byVal X as ArrayList)


''添加三项。

X.Add(" Item 1")

X.Add(" Item 2")

X.Add(" Item 3" )


返回

我什么时候应该将传入参数X作为byRef?

谢谢,

Rob Panosh

Hello,

Ok here is the senerio:

.....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Count > 0 ''This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

''Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh

推荐答案

Rob:


我不确定你在问什么,但是一个ArrayList是一个引用类型,所以

即使你按值传递它,你传递的只是对象的参考复制

。对于所有意图和目的,你不会注意到

的差异。是的,如果您传递此引用类型ByVal并且函数

它被传递给它进行任何更改,原始的ArrayList将被更改为
。因此,传递Ref类型byval或byRef

之间的唯一区别在于,在第一种情况下,您传递了Reference的副本,在第二种情况下,您传递了实际引用。如果你想要一个独特的副本,你可以用你喜欢的东西做b $ b,你将需要选择深度克隆它。


HTH,


" Rob Panosh" < RO ************************ @ asdsoftadfdware.com>写在

消息新闻:Om ************* @ TK2MSFTNGP11.phx.gbl ...
Rob:

I''m not sure what you are asking, but an ArrayList is a Reference type, so
even if you pass it by value, all you are passing is a Copy of the REFERENCE
to the object. For all intents and purposes you won''t notice the
difference. Yes, if you pass this reference type ByVal and the function
it''s passed to makes any changes to it, the original ArrayList will be
changed. So the only difference between passing a Ref type byval or byRef
is that in the first case you pass a copy of the Reference, in the second
you pass the actual reference. If you want a unique copy that you can do
with what you please, you''ll probalby need to opt for a deep clone of it.

HTH,

Bill
"Rob Panosh" <ro************************@asdsoftadfdware.com> wrote in
message news:Om*************@TK2MSFTNGP11.phx.gbl...
你好,

好的,这是senerio:

....

将myArrayList变为新的ArrayList(0)

me.Test_A( myArrayList)

myArralist.Count> 0''这将是正确的。

Public Sub Test_A(byVal X as ArrayList)

''添加三个项目。
X.Add(" Item 1")
X.Add(" Item 2")
X.Add(" Item 3")


什么时候应该我将传入参数X设为byRef?

谢谢,
Rob Panosh
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Count > 0 ''This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

''Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh



应该如果你要对它进行更改将是一个byref,那将是在该子项之外可见的
... byval只生成数据的副本.. byref

制作一个指向数据的指针,如果你打算在那个测试子项之外可以看到更改,那么这就是你所需要的,或者他们只会得到

抛出作为子出口...为什么你会在那里回来?

Rob Panosh < RO ************************ @ asdsoftadfdware.com>写在

消息新闻:Om ************* @ TK2MSFTNGP11.phx.gbl ...
that should be a byref if you are going to make changes to it that will be
visible outside of that sub... byval only makes a copy of the data.. byref
makes a pointer to the data, which is what you need if you are going to make
changes that are visible outside of that test sub, or they will just get
thrown out as the sub exits... and why do you have return in there?
"Rob Panosh" <ro************************@asdsoftadfdware.com> wrote in
message news:Om*************@TK2MSFTNGP11.phx.gbl...
你好,

好的,这是senerio:

....

将myArrayList变为新的ArrayList(0)

me.Test_A( myArrayList)

myArralist.Count> 0''这将是正确的。

Public Sub Test_A(byVal X as ArrayList)

''添加三个项目。
X.Add(" Item 1")
X.Add(" Item 2")
X.Add(" Item 3")


什么时候应该我将传入参数X设为byRef?

谢谢,
Rob Panosh
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Count > 0 ''This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

''Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh



William,
William,
即使你按值传递它,你传递的只是对象的
REFERENCE的副本。
这就是我的要求。我可以看出它们之间的区别。那么

这里有任何影响吗?

我的猜测byRef会有更好的表现,因为它没有必要复制

a复制的参考。


感谢您的帮助和及时的回复


Rob

" William Ryan" <做******** @ comcast.nospam.net>在消息中写道

新闻:美国************** @ TK2MSFTNGP09.phx.gbl ... Rob:

我是'我不确定你在问什么,但是ArrayList是一个引用类型,所以即使你按值传递它,所有你传递的都是对象的
REFERENCE的副本。对于所有意图和目的,你不会注意到
的区别。是的,如果您传递此引用类型ByVal并且传递它的函数
对其进行任何更改,则原始ArrayList将被更改。因此,传递Ref类型byval或byRef
之间的唯一区别在于,在第一种情况下,您传递了Reference的副本,在第二种情况下,您传递了实际引用。如果你想要一份你可以随心所欲的独特副本,你需要选择深入克隆它。

HTH,
Rob Panosh < RO ************************ @ asdsoftadfdware.com>在
消息新闻中写道:Om ************* @ TK2MSFTNGP11.phx.gbl ...
even if you pass it by value, all you are passing is a Copy of the REFERENCE to the object. That is what I was asking. I could see a difference between them. So are
there any peformance implications here?
My guess byRef would have better performance because it doesn''t have to make
a copied of the reference.

Thanks for your help and timely reponse

Rob
"William Ryan" <do********@comcast.nospam.net> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl... Rob:

I''m not sure what you are asking, but an ArrayList is a Reference type, so
even if you pass it by value, all you are passing is a Copy of the REFERENCE to the object. For all intents and purposes you won''t notice the
difference. Yes, if you pass this reference type ByVal and the function
it''s passed to makes any changes to it, the original ArrayList will be
changed. So the only difference between passing a Ref type byval or byRef
is that in the first case you pass a copy of the Reference, in the second
you pass the actual reference. If you want a unique copy that you can do
with what you please, you''ll probalby need to opt for a deep clone of it.

HTH,

Bill
"Rob Panosh" <ro************************@asdsoftadfdware.com> wrote in
message news:Om*************@TK2MSFTNGP11.phx.gbl...
你好,

好的,这是senerio:

....

将myArrayList调暗为新的ArrayList(0)

me.Test_A(myArrayList)

myArralist.Count> 0''这将是正确的。

Public Sub Test_A(byVal X as ArrayList)

''添加三个项目。
X.Add(" Item 1")
X.Add(" Item 2")
X.Add(" Item 3")


什么时候应该我将传入参数X设为byRef?

谢谢,
Rob Panosh
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Count > 0 ''This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

''Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh




这篇关于byVal Vs.按地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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