?
Solved

best way to do this

Posted on 2013-10-29
2
Medium Priority
?
269 Views
Last Modified: 2013-11-04
Please  let me know best way  to do this

Please look at the below c# code.  In the update method whenever the condition fails I need to quit the applications so I used   System.Environment.Exit(1);   and
                       return;  in all the if conditions. This is just a sample but in my update method I have so many   System.Environment.Exit(1); and    return;  statement. It seems to be like repetitive. Can I create a common method like   appExit() and avoid the repetitive lines.
What is the best way to achieve this ?

                       
public partial class employeeUpdate: Form
    {
        public employeeUpdate()
        {
              InitializeComponent();
               update();


          }


    private void update()
        {


             if (a==b)
               {
                      messsagebox("some message");
 
                      System.Environment.Exit(1);
                        return;

               }
                   

                if ( g==h)
               {
                      messsagebox("some message");
 
                      System.Environment.Exit(1);
                        return;

               }

                     if ( z==i)
               {
                      messsagebox("some message");
 
                      System.Environment.Exit(1);
                        return;

               }


         }

Open in new window

0
Comment
Question by:Varshini S
2 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 total points
ID: 39609139
Exit will cause the program to terminate. Period. So your return statements are really extraneous. You can easily demonstrate this:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication22
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Environment.Exit(0);
            }
            finally
            {
                MessageBox.Show("Finally!");
            }
        }
    }
}

Open in new window


Under normal circumstances, you should see a message box. Because Exit is being used, you do not.
0
 
LVL 11

Accepted Solution

by:
Deepak Lakkad earned 1000 total points
ID: 39613768
Hi

Try following code

 private void update()
        {
            int retValue = 0;

             if (a==b)
               {
                      messsagebox("some message");
 
                      retValue = 1;
               }
                   

                if ( g==h)
               {
                      messsagebox("some message");
 
                      retValue = 1;

               }

                     if ( z==i)
               {
                      messsagebox("some message");
 
                     retValue = 1;

               }


               System.Environment.Exit(retValue);
         }

Open in new window


- Deepak Lakkad
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question