故障排除:不包含适用于入口点的静态“主”方法

时间:2013-10-09 20:53:23

标签: c# windows class

我正在尝试创建一个创建学生对象的多类程序,然后允许您为其中一个学生对象更改未声明的专业的值。

这是我的代码:

StudentApp.cs

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

namespace PA04CoddR
{
    class StudentApp
    {

        public void Main()
        {
            DisplayTitle();
            Student firstStudent = new Student("Robert", "Codd");
            DisplayInfo(firstStudent);
            Student secondStudent = new Student("Alexander", "Clemens", "FIN");
            DisplayInfo(secondStudent);
            GetMajor(firstStudent);
            DisplayInfo(firstStudent);
            TerminateProgram();

        }

        public void DisplayTitle()
        {
            Console.WriteLine("Programming Assignment 4 - Creating a Class - Robert S. Codd");
            Console.WriteLine();
            Console.WriteLine("____________________________________________________________");

        }

        public void DisplayInfo(Student newStudent)
        {
            Console.WriteLine("Frist Name: " + newStudent.GetFirstName);
            Console.WriteLine("Last Name: " + newStudent.GetLastName);
            Console.WriteLine("Major: " + newStudent.GetMajor);

        }

        public void GetMajor(Student newStudent)
        {
            Console.WriteLine("\tHello {0} {1}", newStudent.GetFirstName, newStudent.GetLastName);
            Console.WriteLine("\tPlease enter your major: ");
            string majorIn = Console.ReadLine();
            Console.WriteLine();
            newStudent.SetMajor(majorIn);

        }

        public void TerminateProgram()
        {
            Console.WriteLine("___________________________________________________________");
            Console.WriteLine("PRESS ANY KEY TO TERMINATE...");
            Console.Read();

        }
    }
}

Student.CS

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

namespace PA04CoddR
{
    class Student
    {
        private string firstName;
        private string lastName;
        private string major;

        public Student()
        {
        }

        public Student(string firstName, string lastName)
        {
            firstName = GetFirstName;
            lastName = GetLastName;
            major = "Undeclared";
        }

        public Student(string firstName, string lastName, string major)
        {
            firstName = GetFirstName;
            lastName = GetLastName;
            major = GetMajor;
        }

        public string GetFirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
            }
        }

        public string GetLastName
        {
            get
            {
                return lastName;
            }
            set
            {
                lastName = value;
            }
        }

        public string GetMajor
        {
            get
            {
                return major;
            }
            set
            {
                major = value;
            }
        }

        public string SetMajor(string majorIn)
        {
            major = majorIn;
            return majorIn;
        } 
    }
}

我没有在IDE中列出或给出错误,但是一旦我尝试编译程序,它就会返回错误:“不包含适用于入口点的静态'main'方法”

我对这里和其他在线资源进行了一些研究,发现了一些似乎很有希望解决我的问题的事情,比如将main方法更改为static,但是当我尝试我的main方法中的每一件事都返回语法错误时:“非静态字段,方法或属性需要对象引用”

任何帮助都会非常感激,我是一名学习程序员,所以我确信它是相当简单的,我只是不完全理解这些概念。

4 个答案:

答案 0 :(得分:4)

您的主要例程需要是静态的:

 public static void Main()
 {

但是,这样做需要您创建StudentApp的实例:

 public static void Main()
 {
     var app = new StudentApp();
     app.DisplayTitle(); // Call method on the instance

     // Do the same for your other methods...

这是因为您的Main()方法使用的其他方法是实例方法,而不是静态方法。

答案 1 :(得分:1)

你有这个:

public void Main()

但你需要这个:

public static void Main()

答案 2 :(得分:1)

您的Main方法必须是静态的,将StudentApp中的其他方法更改为静态,因为它们不使用任何实例状态。

答案 3 :(得分:1)

您必须将静态添加到public void Main,如下所示:

public static void main(string[]args)
{
   //Your code
}

如果想要使用其他类,则必须向Student类及其所有方法添加静态,并向StudentApp添加。 这是因为静态方法只能调用其他静态方法。