|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Return vs Exit SubIs there any difference between Return and Exit Sub?
I have some code that uses both when I have an error in my Sub. Thanks, Tom Hello tshad,
There is no difference in the IL output from "Return" and "Exit Sub" when used in a sub. However, for readability I would suggtest using Exit Sub. Return denotes pushing a value back to the caller, which since a sub doesnt have a return value, makes for a less clear intent. -Boo Show quoteHide quote > Is there any difference between Return and Exit Sub? > > I have some code that uses both when I have an error in my Sub. > > Thanks, > > Tom > "GhostInAK" <ghosti***@gmail.com> schrieb: Well, you could interpret 'Return' either as "Return something" or "Return >There is no difference in the IL output from "Return" and "Exit Sub" when >used in a sub. However, for readability I would suggtest using Exit Sub. >Return denotes pushing a value back to the caller, which since a sub doesnt >have a return value, makes for a less clear intent. to something". -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Herfried,
> Well, you could interpret 'Return' either as "Return something" or "Return It is not about holidays.> to something". > It is about a nothing returning method in VBNet what has the name Sub. In far past when stacks did not yet be used, there was set a return address mostly as last command in a method. I never investigated this but in a normal modern system, the sub ends and the program goes on in the one higher stack; one position after where it was calling the Sub. :-) CorShow quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht news:eFeg0lKrGHA.3412@TK2MSFTNGP02.phx.gbl... > "GhostInAK" <ghosti***@gmail.com> schrieb: >>There is no difference in the IL output from "Return" and "Exit Sub" when >>used in a sub. However, for readability I would suggtest using Exit Sub. >>Return denotes pushing a value back to the caller, which since a sub >>doesnt have a return value, makes for a less clear intent. > > Well, you could interpret 'Return' either as "Return something" or "Return > to something". > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/>
Show quote
Hide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message There is an implicit return at the end of a Sub. As far as using Exit Subnews:%23gpCRwKrGHA.3256@TK2MSFTNGP04.phx.gbl... > Herfried, > > > Well, you could interpret 'Return' either as "Return something" or "Return > > to something". > > > It is not about holidays. > > It is about a nothing returning method in VBNet what has the name Sub. > > In far past when stacks did not yet be used, there was set a return address > mostly as last command in a method. > > I never investigated this but in a normal modern system, the sub ends and > the program goes on in the one higher stack; one position after where it was > calling the Sub. or Return, I have switched to Return as most other languages use Return for this purpose. VB was the only language to not use return in this manner because the BASIC return statement indicated the return from a GOSUB statement. Mike. Show quoteHide quote > > :-) > > Cor > > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht > news:eFeg0lKrGHA.3412@TK2MSFTNGP02.phx.gbl... > > "GhostInAK" <ghosti***@gmail.com> schrieb: > >>There is no difference in the IL output from "Return" and "Exit Sub" when > >>used in a sub. However, for readability I would suggtest using Exit Sub. > >>Return denotes pushing a value back to the caller, which since a sub > >>doesnt have a return value, makes for a less clear intent. > > > > Well, you could interpret 'Return' either as "Return something" or "Return > > to something". > > > > -- > > M S Herfried K. Wagner > > M V P <URL:http://dotnet.mvps.org/> > > V B <URL:http://classicvb.org/petition/> > > > Michael,
VBNet is the only language where I am/was easily able to go deeper than 8 levels. That because of the nice descriptions of the levels. :-) CorShow quoteHide quote "Michael D. Ober" <ober***@.alum.mit.edu.nospam> schreef in bericht news:V63wg.2752$157.424@newsread3.news.pas.earthlink.net... > > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message > news:%23gpCRwKrGHA.3256@TK2MSFTNGP04.phx.gbl... >> Herfried, >> >> > Well, you could interpret 'Return' either as "Return something" or > "Return >> > to something". >> > >> It is not about holidays. >> >> It is about a nothing returning method in VBNet what has the name Sub. >> >> In far past when stacks did not yet be used, there was set a return > address >> mostly as last command in a method. >> >> I never investigated this but in a normal modern system, the sub ends and >> the program goes on in the one higher stack; one position after where it > was >> calling the Sub. > > There is an implicit return at the end of a Sub. As far as using Exit Sub > or Return, I have switched to Return as most other languages use Return > for > this purpose. VB was the only language to not use return in this manner > because the BASIC return statement indicated the return from a GOSUB > statement. > > Mike. > >> >> :-) >> >> Cor >> >> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht >> news:eFeg0lKrGHA.3412@TK2MSFTNGP02.phx.gbl... >> > "GhostInAK" <ghosti***@gmail.com> schrieb: >> >>There is no difference in the IL output from "Return" and "Exit Sub" > when >> >>used in a sub. However, for readability I would suggtest using Exit > Sub. >> >>Return denotes pushing a value back to the caller, which since a sub >> >>doesnt have a return value, makes for a less clear intent. >> > >> > Well, you could interpret 'Return' either as "Return something" or > "Return >> > to something". >> > >> > -- >> > M S Herfried K. Wagner >> > M V P <URL:http://dotnet.mvps.org/> >> > V B <URL:http://classicvb.org/petition/> >> >> >> > > > Cor,
I assume you mean Exit For Exit Do Exit ... Yes, this is nice, but when I always comment the inner exits. Otherwise, they are tough to track. Mike. Show quoteHide quote "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message news:uKP0fYMrGHA.1140@TK2MSFTNGP05.phx.gbl... > Michael, > > VBNet is the only language where I am/was easily able to go deeper than 8 > levels. That because of the nice descriptions of the levels. > > :-) > > Cor > > "Michael D. Ober" <ober***@.alum.mit.edu.nospam> schreef in bericht > news:V63wg.2752$157.424@newsread3.news.pas.earthlink.net... >> >> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message >> news:%23gpCRwKrGHA.3256@TK2MSFTNGP04.phx.gbl... >>> Herfried, >>> >>> > Well, you could interpret 'Return' either as "Return something" or >> "Return >>> > to something". >>> > >>> It is not about holidays. >>> >>> It is about a nothing returning method in VBNet what has the name Sub. >>> >>> In far past when stacks did not yet be used, there was set a return >> address >>> mostly as last command in a method. >>> >>> I never investigated this but in a normal modern system, the sub ends >>> and >>> the program goes on in the one higher stack; one position after where it >> was >>> calling the Sub. >> >> There is an implicit return at the end of a Sub. As far as using Exit >> Sub >> or Return, I have switched to Return as most other languages use Return >> for >> this purpose. VB was the only language to not use return in this manner >> because the BASIC return statement indicated the return from a GOSUB >> statement. >> >> Mike. >> >>> >>> :-) >>> >>> Cor >>> >>> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht >>> news:eFeg0lKrGHA.3412@TK2MSFTNGP02.phx.gbl... >>> > "GhostInAK" <ghosti***@gmail.com> schrieb: >>> >>There is no difference in the IL output from "Return" and "Exit Sub" >> when >>> >>used in a sub. However, for readability I would suggtest using Exit >> Sub. >>> >>Return denotes pushing a value back to the caller, which since a sub >>> >>doesnt have a return value, makes for a less clear intent. >>> > >>> > Well, you could interpret 'Return' either as "Return something" or >> "Return >>> > to something". >>> > >>> > -- >>> > M S Herfried K. Wagner >>> > M V P <URL:http://dotnet.mvps.org/> >>> > V B <URL:http://classicvb.org/petition/> >>> >>> >>> >> >> >> > > Hello Herfried K. Wagner [MVP],
While one could interpret the meaning in both ways when used in a sub.. Why cause the confusion? In a sub use Exit Sub and in a function use Return. Then there is no ambiguity. Yes, we all know it's equivalent if we bothered to read the documentation (everyone's F1 key borke?) ms-help://MS.VSCC.2003/MS.MSDNQTR.2005APR.1033/vblr7/html/vastmReturn.htm Just because you can do a thing doesn't mean you should do a thing. -Boo Show quoteHide quote > "GhostInAK" <ghosti***@gmail.com> schrieb: > >> There is no difference in the IL output from "Return" and "Exit Sub" >> when used in a sub. However, for readability I would suggtest using >> Exit Sub. Return denotes pushing a value back to the caller, which >> since a sub doesnt have a return value, makes for a less clear >> intent. >> > Well, you could interpret 'Return' either as "Return something" or > "Return to something". > Boo,
I view Return as returning to whence I came. Which as Michael suggests is common across most other languages. Optionally a value may be included when I return to whence I came. Unfortunately I am inconsistent on always using Return; I occasionally use Exit Sub... Which could be considered an odd-ball solution... -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "GhostInAK" <ghosti***@gmail.com> wrote in message news:c71747b4294548c87a4b3ba49594@news.microsoft.com... | Hello tshad, | | There is no difference in the IL output from "Return" and "Exit Sub" when | used in a sub. However, for readability I would suggtest using Exit Sub. | Return denotes pushing a value back to the caller, which since a sub doesnt | have a return value, makes for a less clear intent. | | -Boo | | > Is there any difference between Return and Exit Sub? | > | > I have some code that uses both when I have an error in my Sub. | > | > Thanks, | > | > Tom | > | | Jay,
Have a look at my reply to Michael, otherwise I would agree with you. :-) CorShow quoteHide quote "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schreef in bericht news:uXIs05LrGHA.3592@TK2MSFTNGP02.phx.gbl... > Boo, > I view Return as returning to whence I came. Which as Michael suggests is > common across most other languages. > > Optionally a value may be included when I return to whence I came. > > Unfortunately I am inconsistent on always using Return; I occasionally use > Exit Sub... Which could be considered an odd-ball solution... > > -- > Hope this helps > Jay B. Harlow [MVP - Outlook] > .NET Application Architect, Enthusiast, & Evangelist > T.S. Bradley - http://www.tsbradley.net > > > "GhostInAK" <ghosti***@gmail.com> wrote in message > news:c71747b4294548c87a4b3ba49594@news.microsoft.com... > | Hello tshad, > | > | There is no difference in the IL output from "Return" and "Exit Sub" > when > | used in a sub. However, for readability I would suggtest using Exit > Sub. > | Return denotes pushing a value back to the caller, which since a sub > doesnt > | have a return value, makes for a less clear intent. > | > | -Boo > | > | > Is there any difference between Return and Exit Sub? > | > > | > I have some code that uses both when I have an error in my Sub. > | > > | > Thanks, > | > > | > Tom > | > > | > | > > "GhostInAK" <ghosti***@gmail.com> wrote in message Is that similar tonews:c71747b4294548c87a4b3ba49594@news.microsoft.com... > Hello tshad, > > There is no difference in the IL output from "Return" and "Exit Sub" when > used in a sub. However, for readability I would suggtest using Exit Sub. > Return denotes pushing a value back to the caller, which since a sub > doesnt have a return value, makes for a less clear intent. Function DoSomething () as Boolean if something then DoSomething = true else DoSomething = false end if End Function or Function DoSomething () as Boolean if something then return true else return = false end if End Function or Function DoSomething () as Boolean Dim i as Boolean if something then i = true else i = false end if return i End Function I was never sure which was best as they all work and I tend to do it all three ways. 6 of one... Tom Show quoteHide quote > > -Boo > >> Is there any difference between Return and Exit Sub? >> >> I have some code that uses both when I have an error in my Sub. >> >> Thanks, >> >> Tom >> > > IMO, the first one is bad. I always hated that construct since it looks like
a normal variable assignment and it makes the code a lot harder to understand (I guess they kept it in for compatibility reasons). A simple return is clear and easy to understand. As for the other question of Exit Sub vs Return in subs I tend to use return since it is what both C++ and C# use and I think it looks better. /claes Show quoteHide quote > Function DoSomething () as Boolean > if something then > DoSomething = true > else > DoSomething = false > end if > End Function > > or > > Function DoSomething () as Boolean > if something then > return true > else > return = false > end if > End Function > > or > > Function DoSomething () as Boolean > Dim i as Boolean > if something then > i = true > else > i = false > end if > return i > End Function > > I was never sure which was best as they all work and I tend to do it all > three ways. > > 6 of one... > > Tom > >> >> -Boo >> >>> Is there any difference between Return and Exit Sub? >>> >>> I have some code that uses both when I have an error in my Sub. >>> >>> Thanks, >>> >>> Tom >>> >> >> > > Tshad,
This is a function which should always return something or nothing and than exit. In your first sample you could tend for a exit function, I never use it like that, I will forever use it as in your second sample. The main difference between a Sub and a Function is that a Sub does not return something or nothing. Now I write this, for me is that an extra reason to use Exit Sub in a Sub. Cor Show quoteHide quote "tshad" <tscheider***@ftsolutions.com> schreef in bericht news:%238bwHs2rGHA.2256@TK2MSFTNGP03.phx.gbl... > "GhostInAK" <ghosti***@gmail.com> wrote in message > news:c71747b4294548c87a4b3ba49594@news.microsoft.com... >> Hello tshad, >> >> There is no difference in the IL output from "Return" and "Exit Sub" when >> used in a sub. However, for readability I would suggtest using Exit Sub. >> Return denotes pushing a value back to the caller, which since a sub >> doesnt have a return value, makes for a less clear intent. > > Is that similar to > > Function DoSomething () as Boolean > if something then > DoSomething = true > else > DoSomething = false > end if > End Function > > or > > Function DoSomething () as Boolean > if something then > return true > else > return = false > end if > End Function > > or > > Function DoSomething () as Boolean > Dim i as Boolean > if something then > i = true > else > i = false > end if > return i > End Function > > I was never sure which was best as they all work and I tend to do it all > three ways. > > 6 of one... > > Tom > >> >> -Boo >> >>> Is there any difference between Return and Exit Sub? >>> >>> I have some code that uses both when I have an error in my Sub. >>> >>> Thanks, >>> >>> Tom >>> >> >> > > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message I tend to use 2 or 3, but I might use 1 if I don't want to create a separate news:%239W3lBAsGHA.356@TK2MSFTNGP05.phx.gbl... > Tshad, > > This is a function which should always return something or nothing and > than exit. > > In your first sample you could tend for a exit function, I never use it > like that, I will forever use it as in your second sample. variable just to use as a return value. Tom Show quoteHide quote > > The main difference between a Sub and a Function is that a Sub does not > return something or nothing. > > Now I write this, for me is that an extra reason to use Exit Sub in a Sub. > > Cor > > "tshad" <tscheider***@ftsolutions.com> schreef in bericht > news:%238bwHs2rGHA.2256@TK2MSFTNGP03.phx.gbl... >> "GhostInAK" <ghosti***@gmail.com> wrote in message >> news:c71747b4294548c87a4b3ba49594@news.microsoft.com... >>> Hello tshad, >>> >>> There is no difference in the IL output from "Return" and "Exit Sub" >>> when used in a sub. However, for readability I would suggtest using >>> Exit Sub. Return denotes pushing a value back to the caller, which since >>> a sub doesnt have a return value, makes for a less clear intent. >> >> Is that similar to >> >> Function DoSomething () as Boolean >> if something then >> DoSomething = true >> else >> DoSomething = false >> end if >> End Function >> >> or >> >> Function DoSomething () as Boolean >> if something then >> return true >> else >> return = false >> end if >> End Function >> >> or >> >> Function DoSomething () as Boolean >> Dim i as Boolean >> if something then >> i = true >> else >> i = false >> end if >> return i >> End Function >> >> I was never sure which was best as they all work and I tend to do it all >> three ways. >> >> 6 of one... >> >> Tom >> >>> >>> -Boo >>> >>>> Is there any difference between Return and Exit Sub? >>>> >>>> I have some code that uses both when I have an error in my Sub. >>>> >>>> Thanks, >>>> >>>> Tom >>>> >>> >>> >> >> > > Tshad,
There should be not any difference although I prefer Exit Sub. Cor Show quoteHide quote "tshad" <tscheider***@ftsolutions.com> schreef in bericht news:uRIHHtGrGHA.4992@TK2MSFTNGP05.phx.gbl... > Is there any difference between Return and Exit Sub? > > I have some code that uses both when I have an error in my Sub. > > Thanks, > > Tom >
Replacing a string inside of a PDF
Cut, Copy and Paste? Diagnostics.Process & MSIEXEC problem... How to Return Value from Module? deploy window application handling money SQL and VB Validating all controls on "Save" Detecting when VB.net 2005 splash screen closing or closed Can't delete VB.NET directories & files... How to create a sound at specific frequency? |
|||||||||||||||||||||||