Solved

best way to do this

Posted on 2013-10-29
2
223 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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 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 250 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now