[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

best way to do this

Posted on 2013-10-29
2
Medium Priority
?
261 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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…

650 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