首页 > 解决方案 > CS0120 非静态字段、方法或属性需要对象引用

问题描述

我正在使用表格在 VS 中制作成绩计算应用程序,因此我在尝试让我的班级说话表格时遇到了一点问题,所以它不起作用。我创建了一个方法,以便它包含代码,该代码将计算出包含输入成绩所需的计算。

Form1.cs

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

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

    private void StudentGrade_KeyPress(object sender, KeyPressEventArgs e)
    {
        //allows a decimal point to be added
        var regex = new Regex(@"[^.\s]");

        //only allows numbers and backspace along with the decmial
        if ((!char.IsNumber(e.KeyChar)) && (!char.IsControl(e.KeyChar)) && regex.IsMatch(e.KeyChar.ToString()))
        {
            e.Handled = true;
        }
    }

    private void PaperPrecentage_KeyPress(object sender, KeyPressEventArgs e)
    {
        //onlys allows numbers and backspace
        if ((!char.IsNumber(e.KeyChar)) && (!char.IsControl(e.KeyChar)))
        {
            e.Handled = true;
        }
    }

    public void GradeButton_Click(object sender, EventArgs e)
    {
        try
        {
            //creating the object GradeWorkOut which calls on GradeWorkingOut class
            GradeWorkingOut GradeWorkOut = new GradeWorkingOut();
            GradeWorkOut.Grading();
        }
        catch (Exception)
        {
            MessageBox.Show ("It's not communicating with GradeWorkingOut");
        }
    }

    public void GradesWorkOut()
    {
        GradeWorkingOut.StudentGradeInput = float.Parse(StudentGrade.Text);
    }
}

GradeWorkingOut.cs 此文件的错误第 22 行

    namespace GradeCalculation
{
    class GradeWorkingOut : Form
    {
        public static float StudentGradeInput { get; internal set; }
        public static float StudentPaperInput { get; internal set; }
        public static float Rounding { get; internal set; }
        public static String PaperMark { get; internal set; }
    public void Grading()
    {         
        try
        {
            //Gets the inputted student grade and then converts it from text to a float
            StudentGradeInput = GradeCalculation.GradesWorkOut();
            //Gets the inputted percentage of the paper/assignment and then converts it from text to a float
            StudentPaperInput = float.Parse(PaperPrecentage.Text);
            //This rounds the float to two decimal places 
            Rounding = (float)Math.Round(StudentGradeInput * StudentPaperInput / 100, 2);
            //Outputs the final grade so they know the overall percentage for the paper/assignment            
            OverallGrade.Text = PaperMark = Convert.ToString(Rounding);
            
            //calls the method which contains the alphabetical grade of the user
            AlphabetGrades();
        }
        catch (Exception)
        {
            MessageBox.Show("Please enter a value");
        }

            
        return;
        
    }
    //Contains the Alphabetical Grades in realtion to the percentage the user has
    public void AlphabetGrades()
    {
        try
        {
            String Grade = "";

            switch (Grade)
            {
                case "A+":
                    if (Rounding >= 90)
                    {
                        Grade = "A+";
                    }
                    break;

                case "A":
                    if (Rounding >= 85)
                    {
                        Grade = "A";
                    }
                    break;

                case "A-":
                    if (Rounding >= 80)
                    {
                        Grade = "A-";
                    }
                    break;

                case "B+":
                    if (Rounding >= 75)
                    {
                        Grade = "B+";
                    }
                    break;

                case "B":
                    if (Rounding >= 70)
                    {
                        Grade = "B";
                    }
                    break;

                case "B-":
                    if (Rounding >= 65)
                    {
                        Grade = "B-";
                    }
                    break;

                case "C+":
                    if (Rounding >= 60)
                    {
                        Grade = "C+";
                    }
                    break;

                case "C":
                    if (Rounding >= 55)
                    {
                        Grade = "C";
                    }
                    break;

                case "C-":
                    if (Rounding >= 50)
                    {
                        Grade = "C-";
                    }
                    break;


                case "D":
                    if (Rounding >= 40)
                    {
                        Grade = "D";
                    }
                    break;

                case "F":
                    if (Rounding >= 0)
                    {
                        Grade = "F";
                    }
                    break;
            }
        }
        catch (Exception)
        {

            MessageBox.Show("Unable to show what you grade is in Alphabet form"); ;
        }
    }
}

标签: c#formsvisual-studio

解决方案


非静态字段、方法或属性需要对象引用

发生这种情况是因为您直接调用了您的类而不是通过引用,但您不能这样做。您需要首先创建一个新的实例GradeWorkingOut或使用您在其中创建的引用GradeButton_Click()或将引用作为函数参数传递。

例子:

//******************************************************************
// Pass as a Function argument
//******************************************************************
public void GradesWorkOut(GradeWorkingOut GradeWorkOut) {
    GradeWorkOut.StudentGradeInput = float.Parse(StudentGrade.Text);
}

//******************************************************************
// Use a global reference
//******************************************************************
GradeWorkingOut GradeWorkOut = new GradeWorkingOut();

public void GradesWorkOut() {
    GradeWorkOut.StudentGradeInput = float.Parse(StudentGrade.Text);
}

//******************************************************************
// Create a new Reference
//******************************************************************
public void GradesWorkOut() {
    GradeWorkingOut GradeWorkOut = new GradeWorkingOut();
    GradeWorkOut.StudentGradeInput = float.Parse(StudentGrade.Text);
}

确保使用您调用的相同引用.Grading()和其他功能。


推荐阅读