article

Properties: Not Just For Classes Anymore

Email
Submitted on: 1/18/2015 8:27:00 PM
By: Devin Watson (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: VB 6.0
Views: 1299
 
     Shows a methodology for drastically reducing the amount of time needed to develop "similar but not quite" forms for a type of task.

 
				

Properties: Not Just For Classes Anymore

Often times, I've run into a situation where I say to myself "I wish I could've reused the same form for this purpose." Early on, it was just that; I would copy a form's code and controls over to a new one, adjusting as necessary. Of course, this led to bloated executables and more spaghetti code than I care to remember (it still makes me shudder to think about it.)

However, I did find a unique solution, whilst developing something else entirely: a class module. When developing the properties of a particular class, I wondered if it would be possible to actually do the same to a form. In essence, take a standard VB form and then add my own custom properties. I had done it before using custom methods, but would VB throw up on me for doing something I shouldn't?

Surprisingly, VB didn't complain one bit, and I was able to reduce four forms that were exactly the same except for a few "under-the-hood" changes. These changes were easily be implemented as properties, and what would have taken me several hours to create and debug, now only took 15 minutes. In the rest of this article, I'll discuss and show how this methodology can be used to reduce development time even more, using a simple but powerful example.

Start up Visual Basic with a Standard EXE project. In the project, add another, blank form. Inside this new form, open up the Code Editor window. In the General Declarations area, type the following:

Dim mstrTable As String

And then type:

Public Property Let Table(strTable As String)

You'll pop into the new property's code, just as if you typed it in a class module. Inside this new property, type the following:

Public Property Let Table(strTable As String)

mstrTable = Trim(strTable)

If Not IsValid(mstrTable) Then
     Err.Raise 30000, "Data Table Editor", "Table name appears to be invalid."
     Exit Property
End If

Me.Caption = "Table Editor - " & mstrTable
GetTableItems mstrTable
Me.Visible = True
Me.ZOrder 0

End Property

In the example above, setting the new Table property for the form performs several functions. First, it removes leading and trailing spaces, then checks to see if the name given is, in fact valid ( the function IsValid is not shown here, but rather used to point out that your validation can remain within a form). If the table name is not valid, then an error is raised. This way, the calling form would have something as follows:

On Local Error Resume Next

frmEdit.Table = "monkeys"
If Err.Number = 30000 Then Exit Sub

However, if the table name given is valid, then it goes on to change the caption to reflect the new table name, and the items from that table are retrieved using another private subroutine called GetTableItems. Once that has completed, then the form is made visible, and moved to the forefront of the screen.

Caveats

Using this method can save time by reducing the amount of code you type over and over. However, it is advisable to use the Load statement to cache the form in memory during program load time, so as to avoid any "Object or With Variable Not Set" errors.

Is This The End Of Zombie Shakespeare?

Nope. You can use this method to infinitely extend a form. Just remember to make sure you generalize it enough so that it encompasses the job you want to do, and that you cache it up in memory during program load.

Happy Programming!


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.