473,786 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using GOTO

Hi,
Is it good practice using GOTO in .NET application?

Please advice.

Thanks, Kartic
Nov 20 '05 #1
13 8462
I'm not religious about it, although I don't have a single GOTO in over
50,000 lines of code in my project. It isn't neccessary. But if you feel
the need, be my guest ;)
"Kartic" <kg****@csystem ssoftware.com> wrote in message
news:uC******** ******@TK2MSFTN GP12.phx.gbl...
Hi,
Is it good practice using GOTO in .NET application?

Please advice.

Thanks, Kartic

Nov 20 '05 #2
Hi Kartic,

Is this serious or do you want to build a long thread in this newsgroup.

The answer is No.

Cor
Nov 20 '05 #3
Kartic:

There are a few situations where Goto may be the best solution, like in a
switch statement where you want to simulate fall-through. However, those
situations are Rare indeed and I can count them on my hand. The real
problem with Goto is that people overuse them and use them as slopppy
shortcuts. Like Robin, I think in about 60,000 lines of code, I don't think
I've ever needed to use one, and prefer not to. And my examples of
Fall-through is related to C# mainly and only deals with switch statements.

Basically, the answer is No, don't use it. If you are absolutely sure that
using GoTo is the most effecitve way to do something, then go ahead, but
limit it's use as much as possible and make sure you have a very compelling
reason to use it. And if your program uses it more than once or twice in
rare occassions, you are already overusing it.

I'm not religious about it either, I just don't see much of a need for it...
and I've seen a lot more damage done with it than good come from it.
"Kartic" <kg****@csystem ssoftware.com> wrote in message
news:uC******** ******@TK2MSFTN GP12.phx.gbl...
Hi,
Is it good practice using GOTO in .NET application?

Please advice.

Thanks, Kartic

Nov 20 '05 #4
Kartic,
As the others suggest, most developers feel using GOTO is not a good
practice in any language!

However! using GOTO is useful sometimes.

One place I see value is a Try/Catch block to allow retrying a statement,
something like:

Try
TRYAGAIN:
AttemptToReadFi le()
' Throw New System.IO.FileN otFoundExceptio n
Catch ex As System.IO.FileN otFoundExceptio n
If MessageBox.Show ("What would you like to do?", "File not
found", MessageBoxButto ns.RetryCancel, MessageBoxIcon. Question,
MessageBoxDefau ltButton.Button 2) = DialogResult.Re try Then
GoTo TRYAGAIN
Else
Throw ' let error propagate up
End If
End Try

Where the AttemptToReadFi le method opens a file and attempts to read it, if
the file is not found, a dialog is shown to the user, and the Goto allows
the program to try the AttemptToReadFi le a second time. I would only handle
specific exceptions this way, for example FileNotFoundExc eption instead of
System.Exceptio n.
Another place I see value is in complicated loops, within loops, when you
need to get out of an inner most loop, back to the outer most loop quickly,
however I attempt to avoid nesting loops (directly) too much...

Hope this helps
Jay

"Kartic" <kg****@csystem ssoftware.com> wrote in message
news:uC******** ******@TK2MSFTN GP12.phx.gbl...
Hi,
Is it good practice using GOTO in .NET application?

Please advice.

Thanks, Kartic

Nov 20 '05 #5
Yes, the second example is one I've come across a lot in my code. Instead
of using the GOTO, I tend to use a boolean flag to tell me when to break out
of the loop. Actually, in this case, I would hardly say it was easier to
read or understand the flow of code, but I've just become used to doing
things like this rather than using gotos'.

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:eC******** ******@TK2MSFTN GP12.phx.gbl...
Kartic,
As the others suggest, most developers feel using GOTO is not a good
practice in any language!

However! using GOTO is useful sometimes.

One place I see value is a Try/Catch block to allow retrying a statement,
something like:

Try
TRYAGAIN:
AttemptToReadFi le()
' Throw New System.IO.FileN otFoundExceptio n
Catch ex As System.IO.FileN otFoundExceptio n
If MessageBox.Show ("What would you like to do?", "File not
found", MessageBoxButto ns.RetryCancel, MessageBoxIcon. Question,
MessageBoxDefau ltButton.Button 2) = DialogResult.Re try Then
GoTo TRYAGAIN
Else
Throw ' let error propagate up
End If
End Try

Where the AttemptToReadFi le method opens a file and attempts to read it, if the file is not found, a dialog is shown to the user, and the Goto allows
the program to try the AttemptToReadFi le a second time. I would only handle specific exceptions this way, for example FileNotFoundExc eption instead of
System.Exceptio n.
Another place I see value is in complicated loops, within loops, when you
need to get out of an inner most loop, back to the outer most loop quickly, however I attempt to avoid nesting loops (directly) too much...

Hope this helps
Jay

"Kartic" <kg****@csystem ssoftware.com> wrote in message
news:uC******** ******@TK2MSFTN GP12.phx.gbl...
Hi,
Is it good practice using GOTO in .NET application?

Please advice.

Thanks, Kartic


Nov 20 '05 #6
Hi Jay B. (And Bill)

I never come with documents, however this greath Dutchman will turn around
about your writting.

(He has done very much for modern programming, in my idea most modern
languages derives partialy from his ideas)

http://www.cs.utexas.edu/users/EWD/

http://www.acm.org/classics/oct95/

Cor
Nov 20 '05 #7
Robin,
Agreed.

I would consider the Boolean over the Goto, until the Boolean was
complicating & convoluting the code too much... In that there is stuff after
the inner loops that need to be skipped, so you have to include the Boolean
on each loop, then you need if statements that check the Boolean, then you
need...
Remember bad use of a Goto does not make the Goto bad!

Too many developers think Goto is Bad! as they have seen bad uses of Goto
too often...

Jay

"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:c6******** ***********@new s.demon.co.uk.. .
Yes, the second example is one I've come across a lot in my code. Instead
of using the GOTO, I tend to use a boolean flag to tell me when to break out of the loop. Actually, in this case, I would hardly say it was easier to
read or understand the flow of code, but I've just become used to doing
things like this rather than using gotos'.

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:eC******** ******@TK2MSFTN GP12.phx.gbl...
Kartic,
As the others suggest, most developers feel using GOTO is not a good
practice in any language!

However! using GOTO is useful sometimes.

One place I see value is a Try/Catch block to allow retrying a statement, something like:

Try
TRYAGAIN:
AttemptToReadFi le()
' Throw New System.IO.FileN otFoundExceptio n
Catch ex As System.IO.FileN otFoundExceptio n
If MessageBox.Show ("What would you like to do?", "File not
found", MessageBoxButto ns.RetryCancel, MessageBoxIcon. Question,
MessageBoxDefau ltButton.Button 2) = DialogResult.Re try Then
GoTo TRYAGAIN
Else
Throw ' let error propagate up
End If
End Try

Where the AttemptToReadFi le method opens a file and attempts to read it,

if
the file is not found, a dialog is shown to the user, and the Goto allows the program to try the AttemptToReadFi le a second time. I would only

handle
specific exceptions this way, for example FileNotFoundExc eption instead of System.Exceptio n.
Another place I see value is in complicated loops, within loops, when you need to get out of an inner most loop, back to the outer most loop

quickly,
however I attempt to avoid nesting loops (directly) too much...

Hope this helps
Jay

"Kartic" <kg****@csystem ssoftware.com> wrote in message
news:uC******** ******@TK2MSFTN GP12.phx.gbl...
Hi,
Is it good practice using GOTO in .NET application?

Please advice.

Thanks, Kartic



Nov 20 '05 #8
Cor,
First a question: Why are you attempting to build a long thread? ;-)
As I told Robin:

Remember bad use of a Goto does not make the Goto bad!

Too many developers think Goto is Bad! as they have seen bad uses of Goto
too often...

The second link is interesting, I don't see what you are trying to say with
the first (other then who wrote the second).

Just a thought
Jay

"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:e%******** ********@TK2MSF TNGP11.phx.gbl. ..
Hi Jay B. (And Bill)

I never come with documents, however this greath Dutchman will turn around
about your writting.

(He has done very much for modern programming, in my idea most modern
languages derives partialy from his ideas)

http://www.cs.utexas.edu/users/EWD/

http://www.acm.org/classics/oct95/

Cor

Nov 20 '05 #9
I think though, if I remember my formal specification classes, that using
GOTOs does tend to mess up any mathematical description of your process (not
that we tend to design our applications in "Z" before implementing them in
VB!).

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .
Cor,
First a question: Why are you attempting to build a long thread? ;-)
As I told Robin:

Remember bad use of a Goto does not make the Goto bad!

Too many developers think Goto is Bad! as they have seen bad uses of Goto
too often...

The second link is interesting, I don't see what you are trying to say with the first (other then who wrote the second).

Just a thought
Jay

"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:e%******** ********@TK2MSF TNGP11.phx.gbl. ..
Hi Jay B. (And Bill)

I never come with documents, however this greath Dutchman will turn around about your writting.

(He has done very much for modern programming, in my idea most modern
languages derives partialy from his ideas)

http://www.cs.utexas.edu/users/EWD/

http://www.acm.org/classics/oct95/

Cor


Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
4276
by: Hayri ERDENER | last post by:
hi, what is the equivalent of C languages' goto statement in python? best regards
11
6602
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
9
11475
by: Ed Staffin | last post by:
Hi, In vb6, if I had an error handler I could catch an error, make a correction and then try it again using resume. I want to do the same thing in vb.net. I am trying to deal with some concurrency issues. I want to be able to catch a error, wait a few milliseconds and then try it again. Any help on this would be great. Thanks ... Ed
6
3653
by: eBob.com | last post by:
How do you make a loop iterate early without using a GoTo? (I guess I've done too much structured programming and I really don't like using GoTos.) Here's my code ... For Each Thing As OFI In FileInfo If Thing.Displayed <> True Then GoTo Iterate 'skip this entry; try next one End If
3
6808
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer> </Root> I would like to retreive "\Root\Customer\Name" out of it. Something like:
23
1792
by: marora | last post by:
I keep hearing about not to use 'go_to' in your code. However, I don't know what's the reasoning behind it? Can any one here shed some light? Thanks in advance, Manish
67
7394
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is constantly cursing at the goto statement, accusing it of being some sort of spawn of satan. So it seems we have a pickle here. The thing is, at first thought it seems that there aren't any viable alternatives which are better suited for this...
11
9716
by: O.B. | last post by:
Does C# support anything like PHP's break command that optionally accepts a parameter specifying how many loops to break out of?
1
3520
by: Mientje | last post by:
I've made an Access 2007 database to store information about the lessonplans I have to make every schoolyear. I want to export the data form the table "Lesvoorbereiding" (Lessonplans in English) to Word using a readymade template (NLD-LV1.dotx). All works well except for the multivalued fields 'Lesuur' (Lessonhour) and 'Werkvormen' (Used activities) and the field 'Lesverloop' (Lessonflow?), i.e. a field where you can link to documents, etc. on...
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.