|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Stumped -- trying to update a VBA subroutine for VB.netto 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 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 > "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 Even tho this is a .Net question.....what is keeping you from turning > 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 > > 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. Dan,
> what is keeping you from turning I have this same problem myself. The clue he left that he has the same > your Gosub calls into Function calls ? issue I do is: >> but I have a LOT of variables that are constantly While long parameter lists are possible, they lead to a huge potential for >> being accessed and modified throughout the code so I'm not sure I'd be >> able to provide them as inputs. 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. >
Show quote
Hide quote
"Dan Barclay" <D**@MVPs.org> wrote in Just ANOTHER one of the keywords that have been in the/any BASIC language 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. > 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.
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 Yup. They don't use Basic, they don't understand it, and they don't 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. understand the customer base. What else would we expect? > Well, you shouldn't use them. Unless it makes the code easier to understand > Interesting. I was taught to not use Gosub's...that that's what > function's and subs are for. 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 That's actually very good advice. The number is really "a screenfull" so > lines in each sub/function to some small amount like 30 (? I don't > remember exactly) far as I'm concerned. > which we all know is almost impossible for EVERY Yes, advice and guidelines are just that. You write the code as it needs to > sub/function. 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 DanS wrote:
> Just ANOTHER one of the keywords that have been in the/any BASIC language That's pretty dependent on WHEN you learned BASIC in the first place.> 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. Functions and Subs were relative latecomers to the party. Bob -- Bob O`Bob <filter***@yahoogroups.com> wrote in
Show quoteHide quote news:Ov$meFDKGHA.1728@TK2MSFTNGP14.phx.gbl: True Bob. I first learned Basic on my Commodore 64 back in the early 80's. > 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 No Functions or Subs there. 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 > --
Show quote
Hide quote
"Dan Barclay" <D**@MVPs.org> wrote You didn't mention MS's solution to the same problem....> > 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. 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
Show quote
Hide quote
"Larry Serflaten" <serfla***@usinternet.com> wrote in message Sometimes you can, sometimes it makes more sense to use the nested 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. 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 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)
Array Nomenclature
Problem with NOT Ascertain number of fields in a user defined type How to print a file produced by "Print to File" filemgmt.dll missing error..."Please Help" cast question VBA stealing keys from ActiveX control? make vb6 exe run only on specified computer?... Problem reading from ini file vb.net or c# |
|||||||||||||||||||||||