Terry Kreft said:
Albert,
I think that' the first time I've seen the use GoSub in VB (of course it
was
used all the time in Basic). Now I'm trying to decide whether I like it
or
not.
Yes. You bring up a rather interesting point.
Two things:
1) For any error handling in the vb code, we do have to use a goto. I
don't like it..but, it is part of the landscape that we use.
2) in my example, it is a gosub, not a goto. So, that gosub is really not
MUCH different then if I broke out the code into another separate sub, and
used Call, such as:
Call JumpToIndex(intPos)
So, I am not using a goto, and the syntax is not really much different then
above? So, then why write the code as I did?
The problem here is that of what we call "scope". I made a judgment call
that I wanted to "share" those variables with two routines. I FURTHER wanted
that code to belong ONLY to that one sub.
I have to say that Pascal was really nice in this regards, since one could
write subs inside of subs. The results was that these additional subs would
be LOCAL to the top most routine. This allowed you to "naturally" group a
few functions together (very much like when we place similar groups of code
into a new separate module. And, another example is the difference between
defining variables at the beginning of module, or in each routine (you
choose the appropriate place based on the scope of the variables).
So, that coding example was really a cross over habit of mine from when I
coded in Pascal. However, my "decision" process did come into play..and it
was not a blind shot in the dark. The choice for this was one of scope, and
simply that both bits of code operated on the save variable (intPos). It is
a nice coding technique, since my variables are still ALL local to that one
"main" sub (in this case, the keydown event).
If later on, I need that code for another form, I will NOT have to hunt for
any related routines.
To fair, I certainly could have moved out the local sub JumpToIndex code to
a complete separate sub, and called it. However, my example does show a
"natural" grouping here, and that the sub used *belongs* to the keypress
routine (and, it belongs to NO OTHER CODE).
A developer looking at that code will *instantly* know that this is a local
sub, and is ONLY to be used by the keypress routine. There is NO confusing
as to what routine will use, or call than sub (it is LOCAL to the key down,
and in fact NO other routine can even call it). Further, it actually made
the code easier to read (at least for me!!), and also easier to code. I
wrote the first part "if keycode = 191 then" in my head..and then had to
"think" about the ugly loop part I wrote!!).
You seem to point out that you likely would have moved the code to a
separate routine. In fact, for the most part I would have to agree that your
suggestion is a good idea (in fact, a really good idea). Since that code is
in fact using a BUILT-IN event routine (keydown event), then in fact this
supports even MORE that one should have moved the code out to a separate
routine.
I suppose I can come up with a few more ideas such as:
I also kept the code together in a attempted to reduce the poster having
to deal with more then one routine!!
Looking at the example, there is really not much being shared between the
two routines, so, breaking it out to a separate sub likely would make more
sense here.
However, don't overlook this "natural" grouping of one, or two related subs
inside of a module. It is a natural grouping of code that can help you as a
developer.
If you give me a poker face look, I would have to blink, and concede it
would have been better to written that local sub as a separate sub
routine....