Click here to Skip to main content
14,301,651 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I am trying to access the code in another if condition from another but unable to . could you please suggest an approach. Here is the code i am using

foreach (string arg in Environment.GetCommandLineArgs())
                {
                    if (arg == "–Auto_BlueGrass")
                    {
                        for (int i = 0; i < 2; i++)
                        {
                            if (i==0)
                            {
                                mstrSwitch = "BluegrassCH";
                                AllocConsole();
                                Console.WriteLine("BlueGrass Chargeable Claims Job started in Auto mode - " + DateTime.Now + " ");
                                mblnAutomode = true;
                                goto AutoModePoint;
                            }
                            else if(i==1)
                            {
                                mstrSwitch = "BluegrassRP";
                                AllocConsole();
                                Console.WriteLine("BlueGrass Reporting Claims Job started in Auto mode - " + DateTime.Now + " ");
                                mblnAutomode = true;
                                goto AutoModePoint;
                            }
                        }

                        
                    }



AutoModePoint: if (!IsUserAuthorized())
                {
                    if (mblnAutomode)
                    {
                        Console.WriteLine("You do not have permissions to run this application.  This application will now close." + " " + DateTime.Now);
                        Application.Current.Shutdown();
                    }
                    else
                    {
                        MessageBox.Show("You do not have permissions to run this application.  This application will now close.", "No Access", MessageBoxButton.OK, MessageBoxImage.Stop);
                        Application.Current.Shutdown();
                    }
                }
                else
                {
                    this.DataContext = this;
                    DefinePacificareStateCodes();
                    ReadApplicationSettings();
                }



In the above code first i am using a for condition inside a if and foreach condition.

After execution the steps next to if(i==0) cond it has to goto the if (!IsUserAuthorized()) and execute those steps under that condition.
I tried using the goto statement but in that case it only executes if(i==0) condition but i also want if(i==1) condition to eexecute once the i is incremented in for loop which is not happening using goto statement could anyone please suggets an approach or any idea is appreciated

Aim : suppose i have a x job and x1 and x2 are sub jobs to it instead of executing x1 and x2 individually whenever i click on x first it has to complete x1 and then it should automatically start x2 and complete it.

if question is not understandable please let me know i will modify the question more ..
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Use of goto is proof of bad programming, most of the time. I have never used it, or had to write complex code to avoid using it. Break this up in to methods that you can call, so you can call them in more than one place if you need to. Also, use switch instead of if/else if blocks where you can. You realise that both of your if conditions do the same thing ?
   
Comments
anupama962010 10-Feb-12 7:45am
   
Thank you i will try that way
Amir Mahfoozi 27-Mar-12 5:22am
   
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100