ByVal,ByRef - 我很困惑 [英] ByVal, ByRef - I'm confused

查看:67
本文介绍了ByVal,ByRef - 我很困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


帮助!!!!


我有十万本书试图描述
$ b ByVal和ByRef之间的$ b差异,并且没有一个是我的b $ b。我已经收集到ByVal制作副本和

ByRef指向某个东西并改变它。

简单数据类型的默认值是ByVal,对于对象,它是ByRef。

我到目前为止是否正确?如果是这样的话,我仍然没有任何线索,因为我可以使用哪个以及用于什么目的。


不知道是什么我的问题是。 。 。也许我的大脑花了一个

加息!


任何人都可以给我的帮助将不胜感激!


Sandy

解决方案

Sandy,

我已经收集到ByVal制作副本和
ByRef指向的东西和改变它。
这是正确的。


简单数据类型的默认值是ByVal,对于对象,它是ByRef。
我到目前为止是否正确?
那是错的。


ByVal& ByRef参数独立于Reference&价值类型。默认情况下,所有

参数都是ByVal传递的,你必须在必要时传递一个参数

ByRef,这时你需要修改调用者

变量。


引用类型是堆上存在的对象。如果我有一个变量

这是一个引用类型并将该变量赋值给另一个变量。两个

变量都将指向堆上的同一个对象。


Dim x As Person

x = New Person()

Dim y As Person

y = x


x& y是堆上完全相同的Person对象。


值类型不在堆上。如果我有一个值类型变量和

我将它分配给另一个变量,则会生成一个值的副本。


Dim x As Integer

x = 100

Dim y As Integer

y = x


虽然x& y具有值100,它们在物理上是不同的值

作为副本。


现在当您将变量传递给ByVal参数时,副本变量是

。因此,对于参考类型,引用的副本,这意味着

堆上只有一个对象&对该对象的两个引用。

对于值类型,将创建该值的副本。


将变量传递给ByRef参数时引用这个变量是

。因此,对于参考类型,您可以引用对

对象的引用,对于值类型,您可以引用该值。


记住ByVal &安培; ByRef是参数传递的方式。参考&价值

类型是如何存储数量。


希望这有帮助

Jay


桑迪 <一个******* @ discussions.microsoft.com>在消息中写道

news:05 **************************** @ phx.gbl ...你好帮助!!!!

我有十本书,试图描述ByVal和ByRef之间的区别,但没有一本是
对我来说很清楚我已经收集到ByVal制作副本和ByRef指向的东西并改变它。
简单数据类型的默认值是ByVal,对于对象,它是ByRef。
到目前为止我是否正确?如果是这样的话,我仍然不知道什么时候可以使用哪个以及用于什么目的。

我不知道我的问题是什么。 。 。也许我的大脑需要徒步旅行!

任何人都可以给予我的帮助将不胜感激!

Sandy



Sandy,

我已经收集到ByVal制作副本并且ByRef指向某个东西并进行更改。
这是正确的。


简单数据类型的默认值是ByVal,对于对象,它是ByRef。
我到目前为止是否正确?
那是错的。


ByVal& ByRef参数独立于Reference&价值类型。默认情况下,所有

参数都是ByVal传递的,你必须在必要时传递一个参数

ByRef,这时你需要修改调用者

变量。


引用类型是堆上存在的对象。如果我有一个变量

这是一个引用类型并将该变量赋值给另一个变量。两个

变量都将指向堆上的同一个对象。


Dim x As Person

x = New Person()

Dim y As Person

y = x


x& y是堆上完全相同的Person对象。


值类型不在堆上。如果我有一个值类型变量和

我将它分配给另一个变量,则会生成一个值的副本。


Dim x As Integer

x = 100

Dim y As Integer

y = x


虽然x& y具有值100,它们在物理上是不同的值

作为副本。


现在当您将变量传递给ByVal参数时,副本变量是

。因此,对于参考类型,引用的副本,这意味着

堆上只有一个对象&对该对象的两个引用。

对于值类型,将创建该值的副本。


将变量传递给ByRef参数时引用这个变量是

。因此,对于参考类型,您可以引用对

对象的引用,对于值类型,您可以引用该值。


记住ByVal &安培; ByRef是参数传递的方式。参考&价值

类型是如何存储数量。


希望这有帮助

Jay


桑迪 <一个******* @ discussions.microsoft.com>在消息中写道

news:05 **************************** @ phx.gbl ...你好帮助!!!!

我有十本书,试图描述ByVal和ByRef之间的区别,但没有一本是
对我来说很清楚我已经收集到ByVal制作副本和ByRef指向的东西并改变它。
简单数据类型的默认值是ByVal,对于对象,它是ByRef。
到目前为止我是否正确?如果是这样的话,我仍然不知道什么时候可以使用哪个以及用于什么目的。

我不知道我的问题是什么。 。 。也许我的大脑需要徒步旅行!

任何人都可以给予我的帮助将不胜感激!

Sandy





" Sandy" <一个******* @ discussions.microsoft.com>在留言中写道

news:05 **************************** @ phx.gbl ... < blockquote class =post_quotes>你好!

帮助!!!!

我有十万本书试图描述ByVal和ByRef之间的区别他们中没有一个对我来说很清楚。我已经收集到ByVal制作副本和ByRef指向的东西并改变它。
简单数据类型的默认值是ByVal,对于对象,它是ByRef。
到目前为止我是否正确?如果是这样的话,我仍然不知道什么时候可以使用哪个以及用于什么目的。

我不知道我的问题是什么。 。 。也许我的大脑开始徒步了!

任何人都可以给予我的帮助将不胜感激!

Sandy



首先,你说的是正确的。到目前为止一直很好。

有两个竞争问题可能导致你行使选择

(ByVal或ByRef)

1.数据安全

2.效率

让我们以一个简单的整数为例:

将myInt调整为整数

myInt = 33

如果我们将myInt传递给子程序(或函数程序)ByVal,我们将传递myInt(33)内容的副本
。子过程得到

值33,如果子过程中的代码改变了该值,原始的

变量及其值不受影响。原始变量

中的数据保证不会被无意更改。

如果我们将myInt传递给子程序(或函数程序)ByRef,

subprocedure获取存储原始变量的地址,因此可以改变原始变量的内容。如果发生这种情况

无意中原始数据已损坏。如果它故意完成

,这是在单个

程序中修改多个变量的好方法(通过几个ByRef)。

现在,效率考虑。对象默认传递ByRef,

因为对象通常很大。复制对象中的所有数据

是低效的。当一个对象通过ByRef时,只传递一个4字节的十六进制

地址。效率更高。原始数据的安全性为

程序中的代码风险。通过精心设计的

类来控制对象中数据成员的访问,从而最大限度地降低风险。

由于整数等原始数据类型很小,因此效率
差异很小。

底线:除非你有充分的理由,原语的默认值和

对象才有意义。

您可能希望编写一些简单的过程并将数据传递给它们两种方式,然后修改过程中的数据并查看

原始参数的效果。


-

彼得 - [MVP - .NET Academic]


Hello!

Help!!!!

I have ten zillion books that attempt to describe the
difference between ByVal and ByRef and none of them are
clear to me. I have gathered that ByVal makes a copy and
ByRef points to something and changes it. The default for
simple data types is ByVal and for objects, it''s ByRef.
Am I correct so far? If so, I still don''t have a clue as
to when I might use which and for what purpose.

Don''t know what my problem is . . . maybe my brain took a
hike!

Any help anyone can give me will be greatly appreciated!

Sandy

解决方案

Sandy,

I have gathered that ByVal makes a copy and
ByRef points to something and changes it. That is correct.
The default for
simple data types is ByVal and for objects, it''s ByRef.
Am I correct so far? That is wrong.

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters by default are passed ByVal, you should only pass a parameter
ByRef when you have to, which is when you need to modify the callers
variable.

A Reference Type is an object that exists on the heap. If I have a variable
that is a reference type and assign the variable to another variable. Both
variables will be pointing to the same object on the heap.

Dim x As Person
x = New Person()
Dim y As Person
y = x

Both x & y are the exact same Person object on the heap.

A Value Type does not live on the Heap. If I have a value type variable and
I assign it to another variable, a copy of the value is made.

Dim x As Integer
x = 100
Dim y As Integer
y = x

Although both x & y have the value 100, they are physically different values
as a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable is
made. So for a Reference Type a copy of the reference is made, which means
there is still only one object on the heap & two references to that object.
For a Value Type a copy of the value is made.

When you pass a variable to a ByRef parameter a reference to that variable
is made. So for a Reference Type you have a reference to a reference to the
object, for a Value Type you have a reference to the value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

Hope this helps
Jay

"Sandy" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl... Hello!

Help!!!!

I have ten zillion books that attempt to describe the
difference between ByVal and ByRef and none of them are
clear to me. I have gathered that ByVal makes a copy and
ByRef points to something and changes it. The default for
simple data types is ByVal and for objects, it''s ByRef.
Am I correct so far? If so, I still don''t have a clue as
to when I might use which and for what purpose.

Don''t know what my problem is . . . maybe my brain took a
hike!

Any help anyone can give me will be greatly appreciated!

Sandy



Sandy,

I have gathered that ByVal makes a copy and
ByRef points to something and changes it. That is correct.
The default for
simple data types is ByVal and for objects, it''s ByRef.
Am I correct so far? That is wrong.

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters by default are passed ByVal, you should only pass a parameter
ByRef when you have to, which is when you need to modify the callers
variable.

A Reference Type is an object that exists on the heap. If I have a variable
that is a reference type and assign the variable to another variable. Both
variables will be pointing to the same object on the heap.

Dim x As Person
x = New Person()
Dim y As Person
y = x

Both x & y are the exact same Person object on the heap.

A Value Type does not live on the Heap. If I have a value type variable and
I assign it to another variable, a copy of the value is made.

Dim x As Integer
x = 100
Dim y As Integer
y = x

Although both x & y have the value 100, they are physically different values
as a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable is
made. So for a Reference Type a copy of the reference is made, which means
there is still only one object on the heap & two references to that object.
For a Value Type a copy of the value is made.

When you pass a variable to a ByRef parameter a reference to that variable
is made. So for a Reference Type you have a reference to a reference to the
object, for a Value Type you have a reference to the value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

Hope this helps
Jay

"Sandy" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl... Hello!

Help!!!!

I have ten zillion books that attempt to describe the
difference between ByVal and ByRef and none of them are
clear to me. I have gathered that ByVal makes a copy and
ByRef points to something and changes it. The default for
simple data types is ByVal and for objects, it''s ByRef.
Am I correct so far? If so, I still don''t have a clue as
to when I might use which and for what purpose.

Don''t know what my problem is . . . maybe my brain took a
hike!

Any help anyone can give me will be greatly appreciated!

Sandy




"Sandy" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...

Hello!

Help!!!!

I have ten zillion books that attempt to describe the
difference between ByVal and ByRef and none of them are
clear to me. I have gathered that ByVal makes a copy and
ByRef points to something and changes it. The default for
simple data types is ByVal and for objects, it''s ByRef.
Am I correct so far? If so, I still don''t have a clue as
to when I might use which and for what purpose.

Don''t know what my problem is . . . maybe my brain took a
hike!

Any help anyone can give me will be greatly appreciated!

Sandy



First, you are correct in what you say above. So far so good.
There are two competing issues that might cause you to exercise your options
(ByVal or ByRef)
1. Data safety
2. Efficiency
Let''s take the case of a simple integer:
Dim myInt as Integer
myInt = 33
If we pass myInt to a subprocedure (or function procedure) ByVal, we are
passing a copy of the contents of myInt (33). The subprocedure gets the
value 33, and if code in the subprocedure alters that value, the original
variable and its value are unaffected. The data in the original variable
remains safe from inadvertent alteration.
If we pass myInt to a subprocedure (or a function procedure) ByRef, the
subprocedure gets the address where the original variable is stored and can,
therefore, alter the contents of the original variable. If this happens
unintentionally, original data has been corrupted. If it''s done
intentionally, it''s a great way to modify multiple variables in a single
procedure (pass several ByRef).
Now, the efficiency consideration. Objects are passed ByRef as default,
because objects are often large. Making a copy of all the data in an object
is inefficient. When an object is passed ByRef, only a 4 byte hexadecimal
address is passed. Far more efficient. The safety of the original data is at
some risk to code in the procedure. The risk is minimized by well designed
classes which control access to data members in an object.
As primitive data types such as an integer are small, the efficiency
difference is minimal.
Bottom line: Unless you have good reasons, the defaults for primitives and
objects make sense.
You may want to write a few simple procedures and pass data to them both
ways, then modify the data in the procedure and see the effect on the
original parameters.

--
Peter - [MVP - .NET Academic]


这篇关于ByVal,ByRef - 我很困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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