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

Default Properties tend to hide programming mistakes

Total Hit ( 2857)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Visual Basic lets you create a default property or method by simply selecting the "(Default)" item in the combo box that appear if you click the Advanced button in the Procedure Attributes dialog box. (You can display this dialog from the Tools menu, or by right-clicking on a property name in the right-most pane in the Object Browser.)

While default properties are cool and make your object easier to use, you should be at least aware that they have their downside as well, in that they tend to hide programming errors in the code that uses the object. To prove this, create a CPerson class module, then add a Name property using this single line of code:

Click here to copy the following block
' in the CPerson class module
Public Name As String

Then write this code in a form module
Private Sub Command1_Click()
  Dim A As New CPerson
  Dim B As CPerson
  Dim C As New CPerson
  ' Create three variables that point to the same object
  A.Name = "Joe Doe"
  ' there is a mistake in the following lines: SET is missing
  B = A
  C = A
End Sub

Because of the missing SET statements - by far the most common error when working with objects - the B=A statement raises error 91 "Object variable or With block variable not set", whereas the C=A statement raises a more cryptic error 438 "Object doesn't support this property or method". This is OK, because we made a programming mistake and VB has been as kind.
However, if you now make Name the default property of the CPerson class, you'll find that in some cases no error is raised, thus preventing the alarm bell from ringing in your head. In fact, while the B=A statement still raises error 91, the C=A assignment now executes flawlessly. Unfortunately, though, the results aren't what you expected: VB has silently created a new instance of the CPerson class (the C variable) and has assigned it the default property of the object held in the A variable. In the end you have two objects instead of one, and the code isn't doing what it was supposed to do.

The solution (sort of) is simple: don't create default properties. Or at least, double-check that all assignments between objects are prefixed by the SET command.





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.