无法访问变量 - 脱离上下文

时间:2016-04-07 22:24:00

标签: c#

我一直试图通过搜索和阅读来解决这一问题,而且我知道它必须是简单的,但在最后一点代码的底部,我是尝试创建打印出输入内容的方法,并且我不断收到此错误。

错误CS0103名称' student1'在当前的情境中不存在 错误CS0103名称' teacher1'在当前上下文中不存在 错误CS0103名称' course1'在当前上下文中不存在

如果你能指出我正确的方向或至少给我一个暗示,我将永远感激不尽。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentInformation
{
class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("What would you like to do? Please choose a number: ");
        Console.WriteLine("");
        Console.WriteLine("1. Enter Student Information");
        Console.WriteLine("2. Enter Teacher Information");
        Console.WriteLine("3. Enter Class Information");
        Console.WriteLine();
        int menuOne = Convert.ToInt32(Console.ReadLine());

        if (menuOne == 1)
        {
            GetStudent();
        }
        else if (menuOne == 2)
        {
            GetTeacher();
        }
        else if (menuOne == 3)
        {
            GetClass();
        }
        else
        {
            Console.WriteLine("Incorrect input. Exiting application.");
            Console.ReadLine();
        }

    }


    public class Student
    {
        public string fName { get; set; }
        public string lName { get; set; }
        public string bDate { get; set; }
    }

    public class Teacher
    {
        public string tFName { get; set; }
        public string tLName { get; set; }
        public string tClass { get; set; }
    }

    public class Course
    {
        public string cName { get; set; }
        public string cSubject { get; set; }
        public string cStart { get; set; }
    }

    static void GetTeacher()
    {
        Teacher teacher1 = new Teacher();

        Console.WriteLine("Enter teacher's first name: ");
        teacher1.tFName = Console.ReadLine();
        Console.WriteLine("Enter teacher's last name: ");
        teacher1.tLName = Console.ReadLine();
        Console.WriteLine("Enter teacher's class name: ");
        teacher1.tClass = Console.ReadLine();


    }

    static void GetStudent()
    {
        Student student1 = new Student();

        Console.WriteLine("Enter student's first name: ");
        student1.fName = Console.ReadLine();
        Console.WriteLine("Enter student's last name: ");
        student1.lName = Console.ReadLine();
        Console.WriteLine("Enter student's birthdate: ");
        student1.bDate = Console.ReadLine();
        PrintStudent();

    }

    static string GetClass()
    {
        Course course1 = new Course();

        Console.WriteLine("Enter class name: ");
        course1.cName = Console.ReadLine();
        Console.WriteLine("Enter class subject: ");
        course1.cSubject = Console.ReadLine();
        Console.WriteLine("Enter class start time: ");
        course1.cStart = Console.ReadLine();
        return course1.cName;
    }

    static void PrintStudent()//Going to add PrintTeacher and PrintCourses as well after I figure this out
    {
        //string student1.fName = GetStudent();
        // Console.WriteLine(fName);
        Console.WriteLine("{0} and {1} are in {2}", student1.fname, teacher1.tFName, course1.cName);
    }

}
}

1 个答案:

答案 0 :(得分:2)

您的所有变量course1student1teacher1分别是方法GetClassGetStudentGetTeacher的本地变量。

如果您打算像这样访问它们,您需要使它们成为类变量。但是,更换用以下内容写出详细信息的语句会更简单:

 Console.WriteLine("{0} and {1} are in {2}", GetStudent(), GetTeacher(), GetClass());

然而,这并没有成为" Get"方法需要用户输入。您需要将保存用户输入的变量设置为类变量。

您似乎已经创建了返回这些值的方法,但是您没有使用它们,但是,只有GetClass方法返回一个值。 GetTeacher方法从用户读取值,然后不对其执行任何操作。

最后,您从PrintStudent调用GetStudent方法。如果您想使用该结构,则需要将学生姓名传递给方法:

static void PrintStudent(string studentName)
{
    Console.WriteLine(studentName); // Plus formatting
}

但是,如果你想要打印出班级和老师,那么你将不得不彻底改变你的代码结构 - 否则你最终会得到无限递归,而print方法调用" Get&# 34;调用打印方法的方法....

您需要决定如何构建代码然后保持一致。首先用普通语言写出逻辑 - 这将帮助您确定实际需要的方法和结构。然后,您可以将其转换为伪代码并最终编码。