Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Passing Public class variables by reference

Total Hit ( 2997)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Under VB4 Public variables in form and class modules were implemented in the same manner as variables in BAS modules, that is, as direct references to memory locations. This allowed VB programmers to create procedures that modified values passed by reference, as in:

Click here to copy the following block
'--- the CInvoice class -----
Public Number As Integer

'---- elsewhere in the application
Dim p As New CInvoice
Increment p.Number   ' this works
Print p.Number     ' displays "1"

Sub Increment(value As Long)
  value = value + 1
End Sub

Under VB5 and VB6, Public variables in FRM, CLS and all other types of class modules (that is, all types of VB modules but BAS modules) are internally implemented as a pair of hidden Property Get and Property Let procedures. This means that, when such Public variables are passed as arguments of a procedure, what is actually passed is the return value of a procedure call. Hence, even if the Public property is passed with ByRef, the called procedure acts on a copy of its value, and therefore can't modify the value of the property.
This modified behavior fixes a problem of VB4, which didn't enforce a strict encapsulation of properties implemented as Public variables. However, when porting a program from VB4 to VB5 or VB6 you should pay attention to this issue, because the code might not work any longer. The only workaround is to move the property's value into a temporary variable, and pass such a temporary variable instead, as in:

Click here to copy the following block
Dim temp As Long
temp = p.Number
Increment temp
p.Number = temp

There are two other details to be aware of:

When the Public variable is passed to the external procedure from inside the class module, it is passed by reference, and therefore the procedure can change its value. In other words:

Click here to copy the following block
' inside the CInvoice class module
Increment Number  ' it works!

On the other hand, if you use the Me prefix from inside the class module, the code will access the external property, and therefore the hidden property procedures. In other words:

Click here to copy the following block
' inside the CInvoice class module
Increment Me.Number  ' it doesn't work!


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.