8 Replies - 1680 Views - Last Post: 13 September 2011 - 10:40 AM Rate Topic: -----

#1 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

not all code path return a value/name does not exist

Posted 13 September 2011 - 10:01 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static public class Gradebook
        {
            static double[] alex = new double[3] { 79, 94, 80 };
            static double[] pat = new double[3] { 65, 99, 75 };
            static double[] casey = new double[3] { 100, 94, 99 };
            static double[] sam = new double[3] { 70, 75, 73 };
            static double[] joe = new double[3] { 75, 88, 94 };
            static double[] everyone = new double[15] { 79, 94, 80, 75, 88, 94, 70, 75, 73, 100, 94, 99, 65, 99, 75 };
            static double[] exam1 = new double[5] { 75, 70, 100, 65, 79 };
            static double[] exam2 = new double[5] { 94, 99, 94, 75, 88 };
            static double[] exam3 = new double[5] { 75, 80, 94, 73, 99 };

            static public double studentAverage()
            {
                double alexGrade = alex.Average();
                double patGrade = pat.Average();
                double caseyGrade = casey.Average();
                double samGrade = sam.Average();
                double joeGrade = joe.Average();
            }

            static public double examAverage()
            {
                double exam1Grade = exam1.Average();
                double exam2Grade = exam2.Average();
                double exam3Grade = exam3.Average();
            }
            static public double overallAverage()
            {
                double classGrade = everyone.Average();
            }

            static private void button1_Click(object sender, EventArgs e)
            {
                string input = txtRequest.Text;
                if (input.Equals("pat", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Pat's Average is " + patGrade);
                }
                else if (input.Equals("alex", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Alex's Average is " + alexGrade);
                }
                else if (input.Equals("casey", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Casey's Average is " + caseyGrade);
                }
                else if (input.Equals("sam", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Sam's Average is " + samGrade);
                }
                else if (input.Equals("joe", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Joe's Average is " + joeGrade);
                }
                else if (input.Equals("exam 1", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("The class' Average for the first exam is " + exam1Grade);
                }
                else if (input.Equals("exam 2", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("The class' Average for the second exam is " + exam2Grade);
                }
                else if (input.Equals("exam 3", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("The class' Average for the third exam is " + exam3Grade);
                }
                else if (input.Equals("class", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("The class' Average for all three exams is " + classGrade);
                }
                else
                {
                    Console.WriteLine("Incorrect Input");
                }
            }
        }
    }
}


Error 1 'WindowsFormsApplication1.Form1.Gradebook.studentAverage()': not all code paths return a value 44
Error 2 'WindowsFormsApplication1.Form1.Gradebook.examAverage()': not all code paths return a value 53
Error 3 'WindowsFormsApplication1.Form1.Gradebook.overallAverage()': not all code paths return a value 59
Error 4 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.txtRequest' 66
Error 5 The name 'patGrade' does not exist in the current context 69
Error 6 The name 'alexGrade' does not exist in the current context 73
Error 7 The name 'caseyGrade' does not exist in the current context 77
Error 8 The name 'samGrade' does not exist in the current context 81
Error 9 The name 'joeGrade' does not exist in the current context 85
Error 10 The name 'exam1Grade' does not exist in the current context 89
Error 11 The name 'exam2Grade' does not exist in the current context 93
Error 12 The name 'exam3Grade' does not exist in the current context 97
Error 13 The name 'classGrade' does not exist in the current context 101

Is This A Good Question/Topic? 0
  • +

Replies To: not all code path return a value/name does not exist

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: not all code path return a value/name does not exist

Posted 13 September 2011 - 10:05 AM

static public double studentAverage()
{
    double alexGrade = alex.Average();
    double patGrade = pat.Average();
    double caseyGrade = casey.Average();
    double samGrade = sam.Average();
    double joeGrade = joe.Average();
}

static public double examAverage()
{
    double exam1Grade = exam1.Average();
    double exam2Grade = exam2.Average();
    double exam3Grade = exam3.Average();
}

static public double overallAverage()
{
    double classGrade = everyone.Average();
}



You've told the compiler that each of these functions returns a double to the caller. Where do you return anything? If you don't understand this explanation, go back to your book and notes on functions and read thoroughly.
Was This Post Helpful? 1
  • +
  • -

#3 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: not all code path return a value/name does not exist

Posted 13 September 2011 - 10:14 AM

I thought that returning the averages was returning something.

It's not?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: not all code path return a value/name does not exist

Posted 13 September 2011 - 10:23 AM

View PostJackOfAllTrades, on 13 September 2011 - 11:05 AM, said:

You've told the compiler that each of these functions returns a double to the caller. Where do you return anything? If you don't understand this explanation, go back to your book and notes on functions and read thoroughly.


.Average is a legal method within a double[] as long as you are Using System.Linq.Enumerable

Attached Image

As for the "does not exist in this context...
You defined those variables within a method. Therefore they only exist within that method. When the method ends, those variables go out of scope and cease to exist.

View PostGoff256, on 13 September 2011 - 11:14 AM, said:

I thought that returning the averages was returning something.

It's not?


It isn't magaically deciding for itself what to return. You have to tell it the one value you want returned to the calling method.

return FredAverage;


for example

Just so you understand

10   static public double examAverage()
11{
12    double exam1Grade = exam1.Average();
13    double exam2Grade = exam2.Average();
14    double exam3Grade = exam3.Average();
15
}



This method makes 3 variables and assigns them values.
Then ends. Period. Makes variables. Does math. Does nothing with any of that. Then ends and the variables evaporate.

The method signature says that a double is supposed to be returned.
You have no return statement.

Jack is not wrong, or being mean when he says you need to go back a chapter or two in your textbook and *read* (not skim) the content until you understand it. Future lessons build on these basic concepts. Don't fake it until it becomes clear because you will only get further lost. Build a solid foundation now!
Was This Post Helpful? 1
  • +
  • -

#5 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: not all code path return a value/name does not exist

Posted 13 September 2011 - 10:25 AM

A) I seem to be making these rookie mistakes way too often. I feel a bit stupid.
B)

Warning 1 Unreachable code detected 53
Warning 2 Unreachable code detected 65
Error 3 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.txtRequest' 76

showed up

C) How would I rectify the error about the 'current context'?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: not all code path return a value/name does not exist

Posted 13 September 2011 - 10:32 AM

YOu need to read up about 'scope'. But here's the basics:

A thing exists only within the braces that define the block it was created in.

using System;

public namespace demo
{
    public class Class1
    {
        int AvailableThroughoutClass = 1;

        public void method1()
        {
            int AvailableThroughoutMethod = 2;

            using (int JustWithinUsing = 3)
            {

            }
        }
    }
}




So you need to move anything you want available throughout the class to being created within the class not a method.
Was This Post Helpful? 1
  • +
  • -

#7 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • Posts: 552
  • Joined: 07-July 10

Re: not all code path return a value/name does not exist

Posted 13 September 2011 - 10:36 AM

static private void button1_Click(object sender, EventArgs e)
            {
                string input = txtRequest.Text;
                if (input.Equals("pat", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Pat's Average is " + patGrade);
                }
}

What is patGrade? You have an object called Pat, but nothing called patGrade.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: not all code path return a value/name does not exist

Posted 13 September 2011 - 10:39 AM

The OP made it inside the StudentAverages() method. Thus our exchange about scope.
Was This Post Helpful? 0
  • +
  • -

#9 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: not all code path return a value/name does not exist

Posted 13 September 2011 - 10:40 AM

View PostRobin19, on 13 September 2011 - 10:36 AM, said:

static private void button1_Click(object sender, EventArgs e)
            {
                string input = txtRequest.Text;
                if (input.Equals("pat", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Pat's Average is " + patGrade);
                }
}

What is patGrade? You have an object called Pat, but nothing called patGrade.


double patGrade = pat.Average();

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1