Click to See Complete Forum and Search --> : Standards


Canada Bass
February 3rd, 2000, 01:55 PM
I'd like to ask peoples point of view when calling a Callback. I would think of the callback as the event procedure that is fired by a control. I have seen VB books and code on websites that call the callback from inside another procedure.

In the VB environment a callback is designated by the controls name and the name of the event. Such as a form's load event calls the callback Form_Load. Some callbacks may have paramters that are passed to the procedures.

Since I have developed in languages other then Visual Basic (prior to any VB programming I have learned), it puzzles me as to why a program would reference this callback in the code. The only acceptable practice should be the control calling the procedure. Some programs need to execute the same code that is in a button's click callback. I understand the developers wanting to minimize code duplication. As a matter of fact, minimizing code duplication is a great practice (when practiced correctly).

What I consider the correct practice is not necessarily what others would agree with. I would think that the correct practice is to make the code a separate procedure that can be called by both the callback function and any other procedure that may need to re-execute the same code. That way any differences between the callbacks and the other functions can be handled in their procedures, while the portion that needs to be called from different places is separate. To me a call such as:


private Sub btnProcessDates_Click()
ProcessDates
End Sub

private Sub ReadDates()
...
ProcessDates
...
End Sub

private Sub ProcessDates()
...
(process code)
...
End Sub




is a much better calling convention then:


private Sub btnProcessDates_Click()
...
(process code)
...
End Sub

private Sub ReadDates()
...
btnProcessDates_Click
...
End Sub





But as I stated earlier, I have even seen the second syntax in books. Am I the only one that thinks the second syntax looks bad?

Nick A.
February 4th, 2000, 03:13 AM
Although i've occasionaly written code using the second style, i agree with you that the first style is the correct one. If you decide to change the name of the button or add some parameters in the ReadDates function, the second style will have to be transformed to the first one (there is no other easy way to do it). Both styles will work but the first one is preferable.