Home All Groups Group Topic Archive Search About

Stumped -- trying to update a VBA subroutine for VB.net

Author
27 Jan 2006 11:14 PM
Rey
I'm trying to figure out what I can use in place of the Gosub command
to accomplish this:

For S1=1 to 10
If somecondition then Gosub routine: goto NxtS1
For S2=1 to 10
If somecondition then Gosub routine: goto NxtS2
....
....
....
NxtS2:
Next S2
NxtS1:
Next S1

routine:
For T1=1 to 10
If someothercondition then Return
For T2=1 to 10
If someothercondition then Return
....
....
....
Next T2
Next T1


I first tried to replace 'Gosub' with 'U=x:Goto Routine' and then
replacing 'Return' with another Goto statement based on the value of U.
When I try to do this VB gives me an error message because the other
Goto statement tries to put me back inside the S loop after having left
it.  If I move 'routine' into the middle of the S loop then the first
Goto doesn't work because it's trying to jump into that loop!
Apparently VB.net has a problem with Goto statements taking you inside
of For...Next loops, even if that's where you originally branched off
from.
There's probably some way I could define a function or a subroutine to
accomplish this, but I have a LOT of variables that are constantly
being accessed and modified throughout the code so I'm not sure I'd be
able to provide them as inputs.

Any suggestions would be appreciated!
Rey

Author
27 Jan 2006 11:41 PM
Dan Barclay
Rey,

There is no functional equivalent of GoSub in VB.Net.

You'll have to rewrite the code to not use GoSub.  If you were using Delphi
you could use nested procedures, which can reproduce that functionality (in
either native or .net).

Please don't use GoTo's to replicate this code.

You need to rewrite in a way that either duplicates the code fragments at
the GoTo site, or creates a new sub or function for the GoSub code.  Neither
process is well suited when a number of variables and a fair amount of code
are involved (nobody believes that but someone who has this need).

GoSub is a very nice container for "code only".  Unfortunately, it is GONE
in VB.Net.  Heck, they even reused the Return keyword for something else.
It's stupid but there you are.

Dan


Show quoteHide quote
"Rey" <notronjer***@gmail.com> wrote in message
news:1138403677.759912.222500@f14g2000cwb.googlegroups.com...
> I'm trying to figure out what I can use in place of the Gosub command
> to accomplish this:
>
> For S1=1 to 10
> If somecondition then Gosub routine: goto NxtS1
> For S2=1 to 10
> If somecondition then Gosub routine: goto NxtS2
> ...
> ...
> ...
> NxtS2:
> Next S2
> NxtS1:
> Next S1
>
> routine:
> For T1=1 to 10
> If someothercondition then Return
> For T2=1 to 10
> If someothercondition then Return
> ...
> ...
> ...
> Next T2
> Next T1
>
>
> I first tried to replace 'Gosub' with 'U=x:Goto Routine' and then
> replacing 'Return' with another Goto statement based on the value of U.
> When I try to do this VB gives me an error message because the other
> Goto statement tries to put me back inside the S loop after having left
> it.  If I move 'routine' into the middle of the S loop then the first
> Goto doesn't work because it's trying to jump into that loop!
> Apparently VB.net has a problem with Goto statements taking you inside
> of For...Next loops, even if that's where you originally branched off
> from.
> There's probably some way I could define a function or a subroutine to
> accomplish this, but I have a LOT of variables that are constantly
> being accessed and modified throughout the code so I'm not sure I'd be
> able to provide them as inputs.
>
> Any suggestions would be appreciated!
> Rey
>
Author
28 Jan 2006 12:06 AM
DanS
"Rey" <notronjer***@gmail.com> wrote in news:1138403677.759912.222500
@f14g2000cwb.googlegroups.com:

Show quoteHide quote
> I'm trying to figure out what I can use in place of the Gosub command
> to accomplish this:
>
> For S1=1 to 10
        > If somecondition then Gosub routine: goto NxtS1
        > If somecondition then Gosub routine: goto NxtS2
        > ...
        > NxtS2:
        > Next S2
        > NxtS1:
> Next S1
        >
        > routine:
        > For T1=1 to 10
        > If someothercondition then Return
        > For T2=1 to 10
        > If someothercondition then Return
        > ...
        > ...
        > ...
        > Next T2
        > Next T1
>
>
> I first tried to replace 'Gosub' with 'U=x:Goto Routine' and then
> replacing 'Return' with another Goto statement based on the value of U.
>  When I try to do this VB gives me an error message because the other
> Goto statement tries to put me back inside the S loop after having left
> it.  If I move 'routine' into the middle of the S loop then the first
> Goto doesn't work because it's trying to jump into that loop!
> Apparently VB.net has a problem with Goto statements taking you inside
> of For...Next loops, even if that's where you originally branched off
> from.
> There's probably some way I could define a function or a subroutine to
> accomplish this, but I have a LOT of variables that are constantly
> being accessed and modified throughout the code so I'm not sure I'd be
> able to provide them as inputs.
>
> Any suggestions would be appreciated!
> Rey
>
>

Even tho this is a .Net question.....what is keeping you from turning
your Gosub calls into Function calls ? Just move the code from the Gosub
part into a function and return the values you need.

EVERY program has lots of variables that are constantly being accessed
and modified throughout the code.

To me that seems like some pretty nasty program flow.
Author
28 Jan 2006 9:51 PM
Dan Barclay
Dan,

> what is keeping you from turning
> your Gosub calls into Function calls ?

I have this same problem myself.  The clue he left that he has the same
issue I do is:

>> but I have a LOT of variables that are constantly
>> being accessed and modified throughout the code so I'm not sure I'd be
>> able to provide them as inputs.

While long parameter lists are possible, they lead to a huge potential for
bugs.  Likewise, if any significant amount of code is involved, simply
duplicating the code inline stinks from a maintenance standpoint as well.
GoSub in Basic (or nested procedures in Delphi) are elegant solutions to
this situation.

Another workaround, not so elegant as GoSub, is to put this sub in a module
of its own and make all the shared variables module level.  The "gosubs" can
be coded as functions in the module using the module level vars.  That's
really crude, but it may be better than the alternatives.  It won't work if
you're using recursion, and you'll need to pay attention to
re-initialization of the variables.

GoSub was an important structure lost to VB.Net.

Dan

Show quoteHide quote
"DanS" <t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote in message
news:Xns9758C25F33DA3idispcom@216.196.97.142...
> "Rey" <notronjer***@gmail.com> wrote in news:1138403677.759912.222500
> @f14g2000cwb.googlegroups.com:
>
>> I'm trying to figure out what I can use in place of the Gosub command
>> to accomplish this:
>>
>> For S1=1 to 10
>    > If somecondition then Gosub routine: goto NxtS1
>    > If somecondition then Gosub routine: goto NxtS2
>    > ...
>    > NxtS2:
>    > Next S2
>    > NxtS1:
>> Next S1
>    >
>    > routine:
>    > For T1=1 to 10
>    > If someothercondition then Return
>    > For T2=1 to 10
>    > If someothercondition then Return
>    > ...
>    > ...
>    > ...
>    > Next T2
>    > Next T1
>>
>>
>> I first tried to replace 'Gosub' with 'U=x:Goto Routine' and then
>> replacing 'Return' with another Goto statement based on the value of U.
>>  When I try to do this VB gives me an error message because the other
>> Goto statement tries to put me back inside the S loop after having left
>> it.  If I move 'routine' into the middle of the S loop then the first
>> Goto doesn't work because it's trying to jump into that loop!
>> Apparently VB.net has a problem with Goto statements taking you inside
>> of For...Next loops, even if that's where you originally branched off
>> from.
>> There's probably some way I could define a function or a subroutine to
>> accomplish this, but I have a LOT of variables that are constantly
>> being accessed and modified throughout the code so I'm not sure I'd be
>> able to provide them as inputs.
>>
>> Any suggestions would be appreciated!
>> Rey
>>
>>
>
> Even tho this is a .Net question.....what is keeping you from turning
> your Gosub calls into Function calls ? Just move the code from the Gosub
> part into a function and return the values you need.
>
> EVERY program has lots of variables that are constantly being accessed
> and modified throughout the code.
>
> To me that seems like some pretty nasty program flow.
>
Author
28 Jan 2006 10:39 PM
DanS
Show quote Hide quote
"Dan Barclay" <D**@MVPs.org> wrote in
news:OlF0JUFJGHA.3936@TK2MSFTNGP12.phx.gbl:

> Dan,
>
>> what is keeping you from turning
>> your Gosub calls into Function calls ?
>
> I have this same problem myself.  The clue he left that he has the
> same issue I do is:
>
>>> but I have a LOT of variables that are constantly
>>> being accessed and modified throughout the code so I'm not sure I'd
>>> be able to provide them as inputs.
>
> While long parameter lists are possible, they lead to a huge potential
> for bugs.  Likewise, if any significant amount of code is involved,
> simply duplicating the code inline stinks from a maintenance
> standpoint as well. GoSub in Basic (or nested procedures in Delphi)
> are elegant solutions to this situation.
>
> Another workaround, not so elegant as GoSub, is to put this sub in a
> module of its own and make all the shared variables module level.  The
> "gosubs" can be coded as functions in the module using the module
> level vars.  That's really crude, but it may be better than the
> alternatives.  It won't work if you're using recursion, and you'll
> need to pay attention to re-initialization of the variables.
>
> GoSub was an important structure lost to VB.Net.
>

Just ANOTHER one of the keywords that have been in the/any BASIC language
since the beginnings of BASIC decades ago, that M$ now considers wrong.

Interesting. I was taught to not use Gosub's...that that's what
function's and subs are for.

Of course, I've seen a 100 web pages that say to limit the number of
lines in each sub/function to some small amount like 30 (? I don't
remember exactly) which we all know is almost impossible for EVERY
sub/function.
Author
28 Jan 2006 11:15 PM
Dan Barclay
Show quote Hide quote
"DanS" <t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote in message
news:Xns9759B3BE4C55Bidispcom@216.196.97.142...
> "Dan Barclay" <D**@MVPs.org> wrote in
> news:OlF0JUFJGHA.3936@TK2MSFTNGP12.phx.gbl:
>
>> Dan,
>>
>>> what is keeping you from turning
>>> your Gosub calls into Function calls ?
>>
>> I have this same problem myself.  The clue he left that he has the
>> same issue I do is:
>>
>>>> but I have a LOT of variables that are constantly
>>>> being accessed and modified throughout the code so I'm not sure I'd
>>>> be able to provide them as inputs.
>>
>> While long parameter lists are possible, they lead to a huge potential
>> for bugs.  Likewise, if any significant amount of code is involved,
>> simply duplicating the code inline stinks from a maintenance
>> standpoint as well. GoSub in Basic (or nested procedures in Delphi)
>> are elegant solutions to this situation.
>>
>> Another workaround, not so elegant as GoSub, is to put this sub in a
>> module of its own and make all the shared variables module level.  The
>> "gosubs" can be coded as functions in the module using the module
>> level vars.  That's really crude, but it may be better than the
>> alternatives.  It won't work if you're using recursion, and you'll
>> need to pay attention to re-initialization of the variables.
>>
>> GoSub was an important structure lost to VB.Net.
>>
>
> Just ANOTHER one of the keywords that have been in the/any BASIC language
> since the beginnings of BASIC decades ago, that M$ now considers wrong.

Yup.  They don't use Basic, they don't understand it, and they don't
understand the customer base.  What else would we expect?

>
> Interesting. I was taught to not use Gosub's...that that's what
> function's and subs are for.

Well, you shouldn't use them.  Unless it makes the code easier to understand
and maintain.

Likewise global variables, module level variables, GoTo... the list goes on.

> Of course, I've seen a 100 web pages that say to limit the number of
> lines in each sub/function to some small amount like 30 (? I don't
> remember exactly)

That's actually very good advice.  The number is really "a screenfull" so
far as I'm concerned.

> which we all know is almost impossible for EVERY
> sub/function.

Yes, advice and guidelines are just that.  You write the code as it needs to
be written.

Folks say GoSub is evil.
For certain, repeated code fragments are evil, causing "change it one place
and not another" bugs.
Also very long parameter lists are evil, causing typo and placement bugs.

Sometimes you have to choose.

FWIW, I choose to *not* move to VB.Net.  I'm moving to Delphi, where I can
have it all.  Unfortunately I have to put up with that "begin/end" thing and
some other "decoration", but it's otherwise much more Basic Like than you'd
think.  Oh, and the developers *use* Delphi for Delphi.

Dan
Author
2 Feb 2006 7:46 PM
Bob O`Bob
DanS wrote:

> Just ANOTHER one of the keywords that have been in the/any BASIC language
> since the beginnings of BASIC decades ago, that M$ now considers wrong.
>
> Interesting. I was taught to not use Gosub's...that that's what
> function's and subs are for.

That's pretty dependent on WHEN you learned BASIC in the first place.
Functions and Subs were relative latecomers to the party.


    Bob
--
Author
3 Feb 2006 2:25 AM
DanS
Bob O`Bob <filter***@yahoogroups.com> wrote in
Show quoteHide quote
news:Ov$meFDKGHA.1728@TK2MSFTNGP14.phx.gbl:

> DanS wrote:
>
>> Just ANOTHER one of the keywords that have been in the/any BASIC
>> language since the beginnings of BASIC decades ago, that M$ now
>> considers wrong.
>>
>> Interesting. I was taught to not use Gosub's...that that's what
>> function's and subs are for.
>
> That's pretty dependent on WHEN you learned BASIC in the first place.
> Functions and Subs were relative latecomers to the party.
>
>
>      Bob

True Bob. I first learned Basic on my Commodore 64 back in the early 80's.
No Functions or Subs there.
Author
3 Feb 2006 3:10 AM
Dan Barclay
Right you are.  At one time I had a variable for a subtotal: Sub.   That one
became the target of a mass edit<vbg>.

Of course, DEFFN subs were there a long time.

Dan


Show quoteHide quote
"Bob O`Bob" <filter***@yahoogroups.com> wrote in message
news:Ov$meFDKGHA.1728@TK2MSFTNGP14.phx.gbl...
> DanS wrote:
>
>> Just ANOTHER one of the keywords that have been in the/any BASIC language
>> since the beginnings of BASIC decades ago, that M$ now considers wrong.
>>
>> Interesting. I was taught to not use Gosub's...that that's what
>> function's and subs are for.
>
> That's pretty dependent on WHEN you learned BASIC in the first place.
> Functions and Subs were relative latecomers to the party.
>
>
> Bob
> --
Author
29 Jan 2006 2:35 AM
Larry Serflaten
Show quote Hide quote
"Dan Barclay" <D**@MVPs.org> wrote
> > what is keeping you from turning
> > your Gosub calls into Function calls ?
>
> I have this same problem myself.  The clue he left that he has the same
> issue I do is:
>
> >> but I have a LOT of variables that are constantly
> >> being accessed and modified throughout the code so I'm not sure I'd be
> >> able to provide them as inputs.
....
> Another workaround, not so elegant as GoSub, is to put this sub in a module
> of its own and make all the shared variables module level.  The "gosubs" can
> be coded as functions in the module using the module level vars.  That's
> really crude, but it may be better than the alternatives.  It won't work if
> you're using recursion, and you'll need to pay attention to
> re-initialization of the variables.

You didn't mention MS's solution to the same problem....

How many API functions have you used that would have required a 'long list' of
parameters if they hadn't been neatly wraped up in a UDT?  You can likewise
create a UDT for just that situation where a GoSub is needed, and pass a single
UDT variable to the repeated section of code, factored out in a separate routine.

LFS
Author
29 Jan 2006 3:57 AM
Dan Barclay
Show quote Hide quote
"Larry Serflaten" <serfla***@usinternet.com> wrote in message
news:eJiYkyHJGHA.424@TK2MSFTNGP12.phx.gbl...
>
> "Dan Barclay" <D**@MVPs.org> wrote
>> > what is keeping you from turning
>> > your Gosub calls into Function calls ?
>>
>> I have this same problem myself.  The clue he left that he has the same
>> issue I do is:
>>
>> >> but I have a LOT of variables that are constantly
>> >> being accessed and modified throughout the code so I'm not sure I'd be
>> >> able to provide them as inputs.
> ...
>> Another workaround, not so elegant as GoSub, is to put this sub in a
>> module
>> of its own and make all the shared variables module level.  The "gosubs"
>> can
>> be coded as functions in the module using the module level vars.  That's
>> really crude, but it may be better than the alternatives.  It won't work
>> if
>> you're using recursion, and you'll need to pay attention to
>> re-initialization of the variables.
>
> You didn't mention MS's solution to the same problem....
>
> How many API functions have you used that would have required a 'long
> list' of
> parameters if they hadn't been neatly wraped up in a UDT?  You can
> likewise
> create a UDT for just that situation where a GoSub is needed, and pass a
> single
> UDT variable to the repeated section of code, factored out in a separate
> routine.

Sometimes you can, sometimes it makes more sense to use the nested
procedure.  In fact, I have some that do both.   The UDT's start getting
squirrlly when you're dealing with arrays as well.

Right tool for the job.  Neither option should be off the table as far as
I'm concerned.

Dan
Author
30 Jan 2006 5:02 PM
Paul Clement
On 27 Jan 2006 15:14:37 -0800, "Rey" <notronjer***@gmail.com> wrote:

¤ I'm trying to figure out what I can use in place of the Gosub command
¤ to accomplish this:
¤
¤ For S1=1 to 10
¤ If somecondition then Gosub routine: goto NxtS1
¤ For S2=1 to 10
¤ If somecondition then Gosub routine: goto NxtS2
¤ ...
¤ ...
¤ ...
¤ NxtS2:
¤ Next S2
¤ NxtS1:
¤ Next S1
¤
¤ routine:
¤ For T1=1 to 10
¤ If someothercondition then Return
¤ For T2=1 to 10
¤ If someothercondition then Return
¤ ...
¤ ...
¤ ...
¤ Next T2
¤ Next T1
¤
¤
¤ I first tried to replace 'Gosub' with 'U=x:Goto Routine' and then
¤ replacing 'Return' with another Goto statement based on the value of U.
¤  When I try to do this VB gives me an error message because the other
¤ Goto statement tries to put me back inside the S loop after having left
¤ it.  If I move 'routine' into the middle of the S loop then the first
¤ Goto doesn't work because it's trying to jump into that loop!
¤ Apparently VB.net has a problem with Goto statements taking you inside
¤ of For...Next loops, even if that's where you originally branched off
¤ from.
¤ There's probably some way I could define a function or a subroutine to
¤ accomplish this, but I have a LOT of variables that are constantly
¤ being accessed and modified throughout the code so I'm not sure I'd be
¤ able to provide them as inputs.
¤

I would definitely convert your GoSub code to Subs. If you have shared variables, declare them at
the Module level so that they will be available to the new Subs, or pass them as arguments to the
Subs.

Don't use GoTo as a replacement. It's even less suitable as a GoSub replacement.


Paul
~~~~
Microsoft MVP (Visual Basic)