Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 2nd, 2004, 05:16 PM
Authorized User
 
Join Date: Oct 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default ByRef and ByVal

Can someone explain the difference between ByRef and ByVal using an example. This would be most appreciated.


 
Old November 2nd, 2004, 07:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default


public sub XByRef(byref a as long)
''a is a reference to the original caller variable
a = a + 10
exit sub

public sub XByVal(byval a as long)
''a is a just a copy of the original caller variable
a = a + 10
exit sub

using byref, the variable 'a' is increased by 10 in the caller scope. Using byval, it remains the same.

dim a as long
a = 10
call XByVal(a)
'' a is still 10
call XByRef(a)
'' a is now 10

Use ByRef if you want that the method can change the value of the variable when it returns.

Marco
 
Old November 2nd, 2004, 08:09 PM
Authorized User
 
Join Date: Oct 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Marco,

Thankyou very much for the example






Similar Threads
Thread Thread Starter Forum Replies Last Post
ByRef Argument! Apocolypse2005 Beginning VB 6 11 August 9th, 2007 02:21 PM
Passing ADO Recordset ByVal robzyc Access VBA 2 April 23rd, 2007 09:07 AM
ByRef property rolandatem VB.NET 2002/2003 Basics 1 October 8th, 2005 08:42 AM
byRef ? g2000 Classic ASP Basics 0 August 18th, 2005 09:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.