locked
Byval and ByRef? RRS feed

  • Question

  • User555322487 posted

    Hi Guys, i want to know ,whats difference between ByVal and ByRef in Vb.Net?. Can anyone explain with exmaple program? Thanks in Advance....

    Wednesday, November 19, 2008 2:34 AM

Answers

  • User-990694832 posted

    Ok,

    sub y()

    dim a as integer

    dim b as string

    x(a,b)

     'a now = 2 because pases by ref  and b still has the same value

    end sub

    sub x(byref a as intiger,b as string)

    a = 2

    b do what ever with

    end sub

    byref passes a pionter to the value and byval is basicaly is just a copy of the value and will not retain values set in the second function. 

    Hope this helps

    DK

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 19, 2008 8:50 AM
  • User397347636 posted

    For value types ('Structure', which includes Integer, Long, etc.), a copy is passed if the parameter is ByVal, but for reference types it's a little different.  For reference types the object reference or 'pointer' is always passed, but with ByVal a copy of the reference itself is passed and the actual reference is never passed back changed.  This means that you can modify the public fields of an object even when passed ByVal, but you cannot change which object the reference is pointing to.

    e.g., if 'SomeRefObject' is a type declared with 'Class':

    Sub TestObjectPassingByVal(ByVal o As SomeRefObject)

        o.SomeField = 2 'this change will be seen after the object call

        o = New SomeRefObject 'this will *not* be seen after the object call

    End Sub

    Sub TestObjectPassingByRef(ByRef o As SomeRefObject)

        o.SomeField = 2 'this change will be seen after the object call

        o = New SomeRefObject 'this will be seen after the object call

    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 19, 2008 10:40 AM

All replies

  • User-990694832 posted

    Ok,

    sub y()

    dim a as integer

    dim b as string

    x(a,b)

     'a now = 2 because pases by ref  and b still has the same value

    end sub

    sub x(byref a as intiger,b as string)

    a = 2

    b do what ever with

    end sub

    byref passes a pionter to the value and byval is basicaly is just a copy of the value and will not retain values set in the second function. 

    Hope this helps

    DK

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 19, 2008 8:50 AM
  • User397347636 posted

    For value types ('Structure', which includes Integer, Long, etc.), a copy is passed if the parameter is ByVal, but for reference types it's a little different.  For reference types the object reference or 'pointer' is always passed, but with ByVal a copy of the reference itself is passed and the actual reference is never passed back changed.  This means that you can modify the public fields of an object even when passed ByVal, but you cannot change which object the reference is pointing to.

    e.g., if 'SomeRefObject' is a type declared with 'Class':

    Sub TestObjectPassingByVal(ByVal o As SomeRefObject)

        o.SomeField = 2 'this change will be seen after the object call

        o = New SomeRefObject 'this will *not* be seen after the object call

    End Sub

    Sub TestObjectPassingByRef(ByRef o As SomeRefObject)

        o.SomeField = 2 'this change will be seen after the object call

        o = New SomeRefObject 'this will be seen after the object call

    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 19, 2008 10:40 AM