none
Name does not exist in current context

    Question

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Linq.Enumerable;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            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 };
    
            public class calculations{
    
                public double studentAverage()
                {
                    double alexGrade = alex.Average();
                    double patGrade = pat.Average();
                    double caseyGrade = casey.Average();
                    double samGrade = sam.Average();
                    double joeGrade = joe.Average();
                    return alexGrade;
                    return patGrade;
                    return caseyGrade;
                    return joeGrade;
                    return samGrade;
                }
    
                public double examAverage()
                {
                    double exam1Grade = exam1.Average();
                    double exam2Grade = exam2.Average();
                    double exam3Grade = exam3.Average();
                    return exam1Grade;
                    return exam2Grade;
                    return exam3Grade;
                }
                public double overallAverage()
                {
                    double classGrade = everyone.Average();
                    return classGrade;
                }
                }
                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");
                    }
                }
            }
        }
    }

    Please and thank you. :)

    Tuesday, September 13, 2011 5:51 PM

Answers

  • Your code needs to be restructured to be more abstract but here is a simple example.

     

    public class Gradebook
    {
        static double[] pat = new double[3] { 65, 99, 75 };
    
        public class calculations
        {
            public double patGrade;
    
            public void StudentAverage()
            {
                patGrade = pat.Average();
            }
    
        }
    }
    
    


     

    Tuesday, September 13, 2011 6:36 PM
  • You can only return one value from a method. So the return statements after the first return statemes are unreachable code.
    Tuesday, September 13, 2011 7:01 PM

All replies

  • You'll need to give more information than that. What line is the error on? Is it a compile time or run time error?

    Edit: you can't return more than once from a method, and your arrays contain a lot of duplicated values. What are you trying to do?

    Tuesday, September 13, 2011 5:51 PM
  • My apologies.

     

    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

    Error 4 The name 'patGrade' does not exist in the current context 69
    Error 5 The name 'alexGrade' does not exist in the current context 73
    Error 6 The name 'caseyGrade' does not exist in the current context 77
    Error 7 The name 'samGrade' does not exist in the current context 81
    Error 8 The name 'joeGrade' does not exist in the current context 85
    Error 9 The name 'exam1Grade' does not exist in the current context 89
    Error 10 The name 'exam2Grade' does not exist in the current context 93
    Error 11 The name 'exam3Grade' does not exist in the current context 97
    Error 12 The name 'classGrade' does not exist in the current context 101

    Tuesday, September 13, 2011 6:10 PM
  • You are defined them in a method StudentAverage and trying to access them in the event handler. Declare them as properties of the class calculations.
    Tuesday, September 13, 2011 6:18 PM
  • You are defined them in a method StudentAverage and trying to access them in the event handler. Declare them as properties of the class calculations.


    Okay, I know how to do this...

     

    but let's pretend that I don't.

    Tuesday, September 13, 2011 6:27 PM
  • Your code needs to be restructured to be more abstract but here is a simple example.

     

    public class Gradebook
    {
        static double[] pat = new double[3] { 65, 99, 75 };
    
        public class calculations
        {
            public double patGrade;
    
            public void StudentAverage()
            {
                patGrade = pat.Average();
            }
    
        }
    }
    
    


     

    Tuesday, September 13, 2011 6:36 PM
  • You can only return one value from a method. So the return statements after the first return statemes are unreachable code.
    Tuesday, September 13, 2011 7:01 PM