Solved

best way to do this

Posted on 2013-10-29
2
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

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

Industry Leaders: 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 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