Solved

best way to do this

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

15 Experts available now in Live!

Get 1:1 Help Now