7 Replies - 1079 Views - Last Post: 29 June 2013 - 06:37 AM Rate Topic: -----

#1 pjr51911   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 13

Warning in my windows form program, not sure how to fix it

Posted 28 June 2013 - 11:26 PM

Getting a 5 Warnings in my code "Unreachable code detected". It's a group project we are working on. I didn't write the particular code the warning relates to, but I was tasked to try and solve it. However, I am beyond a beginner at C# coding, and not quite sure how to resolve the issue.


I have attached the zip file with the program. Maybe someone can take a look and point me in the right direction?
Is This A Good Question/Topic? 0
  • +

Replies To: Warning in my windows form program, not sure how to fix it

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Warning in my windows form program, not sure how to fix it

Posted 28 June 2013 - 11:54 PM

What the warning means is there is code that is impossible for it to ever run. Code after a 'return' is the most common thing I've seen.

As there is no code, there's nothing I can show you :)

Post the code where the error occurs and the code block above that.

This post has been edited by Momerath: 28 June 2013 - 11:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 pjr51911   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 13

Re: Warning in my windows form program, not sure how to fix it

Posted 29 June 2013 - 01:25 AM

 private void linkHome_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Home firstform = new Home();
            firstform.Show();
            this.Hide();
        }

        private void Payment_Load(object sender, EventArgs e)
        {
            // Prevent manual input by users
            this.comboBoxStates.DropDownStyle = ComboBoxStyle.DropDownList;
            
            // FOR ORDERS.TXT READING AND DISPLAYING
            // Declare Arrays for TOTAL
            string[] date = new string[1];
            double[] adultTix = new double[1];
            double[] childTix = new double[1];
            double[] seniorTix = new double[1];
            double[] comboTotal = new double[1];

            // Create second filestream and streamreader objects
            FileStream inFile3 = new FileStream("orders.txt", FileMode.Open, FileAccess.Read);
            StreamReader reader3 = new StreamReader(inFile3);

            // Reading data and loading array[b]
            for (x = 0; x < date.Length; ++x)[/b] (LINE WHERE ERROR IS)
            {
                date[x] = reader3.ReadLine();
                adultTix[x] = Convert.ToDouble(reader3.ReadLine());
                childTix[x] = Convert.ToDouble(reader3.ReadLine());
                seniorTix[x] = Convert.ToDouble(reader3.ReadLine());
                comboTotal[x] = Convert.ToDouble(reader3.ReadLine());
                break;
            }
            reader3.Close();
            inFile3.Close();

            adultTotal = adultTix[x] * 10.75;
            childTotal = childTix[x] * 7.25;
            seniorTotal = seniorTix[x] * 9.00;
            subTotal = adultTotal + childTotal + seniorTotal + comboTotal[x];
            taxTotal = subTotal * TAX;
            grandTotal = subTotal + (subTotal * TAX);

            // Display User Data
            lblOrder.Text = String.Format("{0}\n${1}\n${2}\n${3}\n${4}\n${5}\n${6}\n\n${7}", date[x], adultTotal.ToString("F"), childTotal.ToString("F"), seniorTotal.ToString("F"), comboTotal[x].ToString("F"), subTotal.ToString("F"), taxTotal.ToString("F"), grandTotal.ToString("F"));
            
        }




Was This Post Helpful? 0
  • +
  • -

#4 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Warning in my windows form program, not sure how to fix it

Posted 29 June 2013 - 01:31 AM

Because of the break; at the end of your loop block (line 33) the increment clause of the for loop (++x) can never be reached thus the warning.

Now I have to ask, why are you declaring arrays that only have one element? What's the point of making them arrays?
Was This Post Helpful? 0
  • +
  • -

#5 pjr51911   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 13

Re: Warning in my windows form program, not sure how to fix it

Posted 29 June 2013 - 01:58 AM

That I am unsure of. A group member wrote this code and told me to figure out the error -_-
Was This Post Helpful? 0
  • +
  • -

#6 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Warning in my windows form program, not sure how to fix it

Posted 29 June 2013 - 02:27 AM

The break statement is the error :)
Was This Post Helpful? 0
  • +
  • -

#7 Witchking   User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 189
  • Joined: 17-February 13

Re: Warning in my windows form program, not sure how to fix it

Posted 29 June 2013 - 02:51 AM

for (x = 0; x < date.Length; ++x)
{
    //do stuff
    break;
}

You have a loop, with a break statement in it. The break statement causes the program to exit the loop and continue with execution. There are three parts in a for loop declaration: the initialization, the condition, and the afterthought. When the loop first starts the program executes the initialization x = 0. Then each time before looping it checks whether the condition x < date.Length is true. Finally after each looping, unless a break statement is used, the program executes the afterthought ++x. In your code there is a break statement in the loop causing the program execution to leave the loop after its first execution, meaning it never gets to the afterthought.

This post has been edited by Witchking: 29 June 2013 - 02:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Warning in my windows form program, not sure how to fix it

Posted 29 June 2013 - 06:37 AM

View Postpjr51911, on 29 June 2013 - 02:58 AM, said:

That I am unsure of. A group member wrote this code and told me to figure out the error -_-/>


So he was giving you an exercise for learning how to debug?
Placing a breakpoint and executing line by line should have revealed this very basic error.
There are several tutorials on debugging linked below.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

Did you even read about the command you're trying to use, and look at the examples?
C# Language Specification


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Apr 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1