故障排除:不包含适合入口点的静态“main"方法 [英] Troubleshooting: does not contain a static 'main' method suitable for an entry point

查看:31
本文介绍了故障排除:不包含适合入口点的静态“main"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a multi class program that creates student objects then allows you to change the value for the undeclared major for one of the student objects.

这是我的代码:

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’方法"

I have no errors being listed or given in the IDE but as soon as I try to compile the program it returns the error: "Does not contain a static 'main' method suitable for an entry point"

我对这里和其他在线资源进行了一些研究,发现了一些看起来很有希望解决我的问题的方法,例如将 main 方法更改为静态方法,但是我一尝试发现 main 方法中的所有内容都返回了语法错误:非静态字段、方法或属性需要对象引用"

I did some research on here and other online resources and found a few things that seemed promising for solving my problem like changing the main method to static but as soon as I tried that every thing in my main method is returning a syntax error: "An object reference is required for the non-static field, method, or property"

任何帮助都将不胜感激,我是一名正在学习的程序员,所以我确信这是相当简单的事情,我只是不完全理解这些概念.

Any help at all would be greatly appreciated, I am a learning programmer so I'm sure it is something fairly simple and I just do not full understand the concepts.

推荐答案

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

Your main routine needs to be static:

 public static void Main()
 {

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

However, doing that will require you to create an instance of StudentApp:

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

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

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

This is because your other methods that your Main() methods use are instance methods, not static methods.

这篇关于故障排除:不包含适合入口点的静态“main"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆