(转)ByVal和ByRef的区别

上一篇 / 下一篇  2009-03-02 10:03:33 / 个人分类:QTP

ByVal 传送参数内存的一个拷贝给被调用者。也就是说,栈中压入的直接就是所传的值。
ByRef 传送参数内存的实际地址给被调用者。也就是说,栈中压入的是实际内容的地址。被调用者可以直接更改该地址中的内容。

ByVal 可选的。表示该参数按值传递。
ByRef 表示该参数按地址传递。 ByRef 是 Visual Basic 的缺省选项。

ByVal是传递值 源数据不会被修改
你可以把这个值当作自己的局部变量来使用
ByRef是传递地址 , 源数据可能被修改
你对这个变量的操作将对你传入的那个变量产生影响,就像指针的感觉

实例:
sub Add1(ByVal no as int32)
    no=no+100
end sub
sub Add2(ByRef no as int32)
    no=no+100
end sub
private sub button1_click(sender as object,e as eventargs)handles button1.click
    dim a as int32
    a=100
    Add1(a)
    msgbox (\"a的值为:\" & a)     '显示:a的值为100
    Add2(a)
    msgbox (\"a的值为:\" & a)     '显示:a的值为200,因为Add2中的参数no为ByRef,即
                                 '按地址传递,因此在Add2中对no进行修改后,将会导致
                                 '源参数a的值也被修改。
End Sub

——————————————————————————————————————
3、ByVal和ByRef
    ByVal传递的参数值,而ByRef传递的参数的地址。在这里,我们不用去区别传指针/传地址/传引用的不同,在VB里,它们根本就是一个东西的三种不同说法,即使VB的文档里也有地方在混用这些术语(但在C++里的确要区分指针和引用)
    初次接触上面的程序二SwapPtr的朋友,一定要搞清在里面的CopyMemory调用中,在什么地方要加ByVal,什么地方不加(不加ByVal就是使用VB缺省的ByRef)
    准确的理解传值和传地址(指针)的区别,是在VB里正确使用指针的基础。
    现在一个最简单的实验来看这个问题,如下面的程序三:
【程序三】:'体会ByVal和ByRef
    Sub TestCopyMemory()
        Dim k As Long
        k = 5
Note:   CopyMemory ByVal VarPtr(k), 40000, 4
        Debug.Print k
    End Sub

    上面标号Note处的语句的目的,是将k赋值为40000,等同于语句k=40000,你可以在"立即"窗口试验一下,会发现k的值的确成了40000。
    实际上上面这个语句,翻译成白话:
-----------------------------------------------------------------
就是从保存常数40000的临时变量处拷贝4个字节到变量k所在的内存中。
-----------------------------------------------------------------
    现在我们来改变一个Note处的语句,若改成下面的语句:
Note2:   CopyMemory ByVal VarPtr(k), ByVal 40000, 4
    这句话的意思就成了,从地址40000拷贝4个字节到变量k所在的内存中。由于地址40000所在的内存我们无权访问,操作系统会给我们一个Access Violation内存越权访问错误,告诉我们"试图读取位置0x00009c40处内存时出错,该内存不能为'Read'"。
    我们再改成如下的语句看看。
Note3:   CopyMemory VarPtr(k), 40000, 4
    这句话的意思就成了,从保存常数40000的临时变量处拷贝4个字节到到保存变量k所在内存地址值的临时变量处。这不会出出内存越权访问错误,但k的值并没有变。
    我们可以把程序改改以更清楚的休现这种区别,如下面的程序四:
【程序四】:'看看我们的东西被拷贝到哪儿去了
    Sub TestCopyMemory()
        Dim I As Long, k As Long
        k = 5
        I = VarPtr(k)
NOTE4:  CopyMemory I, 40000, 4
        Debug.Print k
        Debug.Print I
        I = VarPtr(k)
NOTE5:  CopyMemory ByVal I, 40000, 4
        Debug.Print k
    End Sub

程序输出:
5
40000
40000
    由于NOTE4处使用缺省的ByRef,传递的是i的地址(也就是指向i的指针),所以常量40000拷贝到了变量i里,因此i的值成了40000,而k的值却没有变化。但是,在NOTE4前有:i=VarPtr(k),本意是要把i本身做为一个指针来使用。这时,我们必须如NOTE5那样用ByVal来传递指针i,由于i是指向变量k的指针,所以最后常量40000被拷贝了变量k里。

引用
ByVal vs ByRef
The difference between ByVal and ByRef is very simple once we recall the subject on pointers. These keywords are simply abbreviations of the full definition of the differences between the two.

ByVal = By Value and ByRef = By Reference

Now we can recall what I said in the previous chapter about C/C++ and pointers. Pointers act as a Reference to a memory address location. In VB however, this means that the reference is occurring in your statement directly at the location of the actual data, and where it is stored.

Selecting a ByVal would thereby mean you are only working on a copy or replica of the data, and not the actual data segment it self.

This has specific implications for how your statements can handle and alter the data in your variable. The ByRef use, would imply that a change performed by the statement of the value you pass, has an affect on the actual data, while the ByVal use would only alter the copy the statement has to work on.

Simply described, if passed ByRef, a procedure can permanently alter the data stored in the variable. Since it also is faster to send a memory address location, rather than passing a copy of a data argument, it is the default value used by Visual Basic.

ByVal和ByRef的区别

TAG:

 

评分:0

我来说两句

Open Toolbar