Re: Call statements



IMHO I think it is a matter of preference and design goals.

A command button event may have specific code and calls to other routines.
Typically, btnX.Value = True can be used when the developer wishes to simulate
a button click, but the event itself may only contain basic code and a call to
a routine that does the real work.

A button click event is a result of an interaction with the user interface, so most
likely the event code willalso interqact with the GUI. A seperate routine usually
does not interact with GUI, getting its values from input parameters. It becomes
-a nice thing- to seperate the GUI stuff from the internal business rules. This can
help in ease of reading, maintaining or enhancing the code.

For example, let's take a simple F to C cnversion. You have a UI with a textbox,
a label and a button. You enter your degrees in F in the textbox, click the button
and the label gets the C equivalent temperature. Your button event could look like:

Private Sub btnCTemp_Click()

'Assume validation done and valid data is in Text1.Text
Label1.Caption = (5 / 9) * (Text1.Text - 32)

End Sub

That will work, but it is not the best way to go. You might add the following to
make it a bit better:

Private Sub btnCTemp_Click()

'Assume validation done and valid data is in Text1.Text
dim dblFTemp as double
dim dblCTemp as double

dblFTemp = cdbl(Text1.Text)

dblCTemp = (5 / 9) * (dblFTemp - 32)

Label1.Caption = CStr(dblCTemp)

End Sub

What if you wanted to make use of you rconversion function from elsewhere in your
code? You could do this:

Text1.Text = 78.5
btnCTemp.Value = True

This will work. ANother way is to encapsulate the conversion logic within a routine:

Public ToCentigrade(byval dblFTemp as double) as double

ToCentigrade = (5 / 9) * (dblFTemp - 32)

end function

An change the button event:

Private Sub btnCTemp_Click()

'Assume validation done and valid data is in Text1.Text
dim dblFTemp as double
dim dblCTemp as double

dblFTemp = cdbl(Text1.Text)

dblCTemp = ToCentigrade(dblFTemp)

Label1.Caption = CStr(dblCTemp)

End Sub


Now you have two choices: You can simulate the button click event by assigning True
to the button's value property (making sure you have valid data in Text1.Text) or you
can simply call the conversion routine from anywhere in your code, completely independent
of the UI.

Saga
--


"Pop`" <nodoby@xxxxxxxxxxxxxxxxxxx> wrote in message news:17MQi.5770$2o1.3334@xxxxxxxxxxx
Hi,

What is the "danger" in calling command buttons or setting their state = true in VB6 code?

I have a fair amount of repetitive code, sometimes exactly what a command button does and so far
I've been creating separate subs to hold such code. e.g. if I have a cmdDoIt_click() I'll create a
'Private Sub DoIt' and dump the code into that for reuse instead of calling the button.

Instead of duplicating in a few instances, just for grins to see what happens, I've been Call-ing
command buttons or setting them to True. So far nothing untoward has happened, but, though I do
NOT Plan to leave that there, I did the test because I've received some pretty strong advice to
NOT call buttons or set them to True; instead, create a separate sub for the code. But there
wasn't any reasoning behind the advice, that I can find now, at least.
Since nothing untoward has happened, then there must be some other, possibly non-techical reason
for not doing that. I'm hoping someone can give me the reasoning behind it. It's a "head-around"
thing<g>.

TIA,

Pop`




.


Quantcast