有人可以帮我解释ByRef和ByVal!? [英] Can someone explain ByRef and ByVal for me please!?

查看:69
本文介绍了有人可以帮我解释ByRef和ByVal!?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我已经在这个项目上工作了8个月,但我仍然没有确定ByVal和ByRef之间的区别。由于VB中的大多数对象都是

引用类型,我发现通过ByVal允许我存储一个

引用我通过val传递的对象,以更改该对象并且对于

更改将反映在参考的调用者副本中(混淆?)。

那么,在这种情况下byref是什么?


我来自C ++背景,其中引用是指针

(kind-of),指针是指针并通过值转储传递对象一个

的堆栈上的东西副本。我不认为VB中的东西是这样的。

ByVal与C ++传递值不同。


有人可以解决我的混乱并告诉我在哪里(除了COM

包装)我真的需要使用byref吗?


谢谢

Robin

Although I''ve been working on this project for 8 months now, I''m still not
sure of the difference between ByVal and ByRef. As most objects in VB are
reference types, passing ByVal I''ve discovered allows me to store a
reference to the object I passed by val, to change that object and for the
change to be reflected in the callers copy of the reference (confused?).
Well, what is byref for in that case?

I''m coming from a C++ background here, where a reference is a pointer
(kind-of), a pointer is a pointer and passing an object by value dumps a
copy of the thing on the stack. I don''t think things work like this in VB.
ByVal is not the same as C++ passing by value.

Can someone clear up my confusion and tell me where (apart from in COM
wrappers) I really need to use byref?

Thanks
Robin

推荐答案




当您传递变量byref时,您可以对其进行更改和

将其传递回子程序的调用者。当你通过它时,你不能对它进行更改。




--------- -------------

" Robin Tucker" <编号********** @ reallyidont.com>写在

消息新闻:cc ******************* @ news.demon.co.uk ...
Hi,

When you pass a variable byref you can make changes to it and
pass it back to caller of the subroutine. When you pass it byval you cant
make changes to it.

Ken
----------------------
"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cc*******************@news.demon.co.uk...
虽然我已经在这个项目上工作了8个月,但我仍然不确定ByVal和ByRef之间的区别。由于VB中的大多数对象都是引用类型,因此我发现通过ByVal可以存储对val传递的对象的引用,以更改该对象和更改
要反映在调用者的引用副本中(困惑?)。
那么,在这种情况下是什么是byref?

我来自C ++背景,其中一个reference是一个指针
(kind-of),一个指针是一个指针,并且按值传递一个对象转储堆栈上的东西的副本。在VB中我不认为这样的事情是这样的。
ByVal与C ++传递的价值不一样。

有人可以清理我的困惑并告诉我在哪里(除了COM
包装)我真的需要使用byref吗?


Robin
Although I''ve been working on this project for 8 months now, I''m still not
sure of the difference between ByVal and ByRef. As most objects in VB are
reference types, passing ByVal I''ve discovered allows me to store a
reference to the object I passed by val, to change that object and for the
change to be reflected in the callers copy of the reference (confused?).
Well, what is byref for in that case?

I''m coming from a C++ background here, where a reference is a pointer
(kind-of), a pointer is a pointer and passing an object by value dumps a
copy of the thing on the stack. I don''t think things work like this in
VB.
ByVal is not the same as C++ passing by value.

Can someone clear up my confusion and tell me where (apart from in COM
wrappers) I really need to use byref?

Thanks
Robin



我可以去吧。


对于引用类型,给定对象的值实际上是一个引用。

因此传递它ByVal或ByRef基本上是一样的。


使用值类型,传递它ByRef涉及对给定对象的引用

然后传递ByVal,就像你说的那样,''把这个东西的副本转储到

堆栈''。


理解的关键ByRef和ByVal知道还有一个是处理引用类型对象或值类型对象的


" Robin Tucker" <编号********** @ reallyidont.com>写在

消息新闻:cc ******************* @ news.demon.co.uk ...
May I have a go please.

With a reference type, the value of a given object is, in fact, a reference.
Therefore passing it ByVal or ByRef is, essentially the same thing.

With a value type, passing it ByRef involves a reference to a given object
whereas passing it ByVal, as you say, ''dumps a copy of the thing on the
stack''.

The key to understanding ByRef and ByVal is in knowing wherther one is
dealing with a reference type object or a value type object.
"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cc*******************@news.demon.co.uk...
虽然我已经在这个项目上工作了8个月,但我仍然不确定ByVal和ByRef之间的区别。由于VB中的大多数对象都是引用类型,因此我发现通过ByVal可以存储对val传递的对象的引用,以更改该对象和更改
要反映在调用者的引用副本中(困惑?)。
那么,在这种情况下是什么是byref?

我来自C ++背景,其中一个reference是一个指针
(kind-of),一个指针是一个指针,并且按值传递一个对象转储堆栈上的东西的副本。在
VB中我不认为这样的事情是这样的。 ByVal与C ++传递的值不同。

有人可以清除我的困惑并告诉我(除了COM
包装器)我真的需要使用byref吗?

谢谢

Robin
Although I''ve been working on this project for 8 months now, I''m still not
sure of the difference between ByVal and ByRef. As most objects in VB are
reference types, passing ByVal I''ve discovered allows me to store a
reference to the object I passed by val, to change that object and for the
change to be reflected in the callers copy of the reference (confused?).
Well, what is byref for in that case?

I''m coming from a C++ background here, where a reference is a pointer
(kind-of), a pointer is a pointer and passing an object by value dumps a
copy of the thing on the stack. I don''t think things work like this in VB. ByVal is not the same as C++ passing by value.

Can someone clear up my confusion and tell me where (apart from in COM
wrappers) I really need to use byref?

Thanks
Robin



嗨罗宾,


这个讨论主题在我看来非常好。

http: //tinyurl.com/32buy


并且全部阅读,尽管我有一点点愚蠢,

甚至很有意思。


Cor
Hi Robin,

This discussion thread shows it really nice in my opinon.

http://tinyurl.com/32buy

And read them all, although there is a (little) stupidity from me in it,
that is even interesting.

Cor


这篇关于有人可以帮我解释ByRef和ByVal!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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