c# - 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"); ;
}
}
}
解决方案
非静态字段、方法或属性需要对象引用
发生这种情况是因为您直接调用了您的类而不是通过引用,但您不能这样做。您需要首先创建一个新的实例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()
和其他功能。
推荐阅读
- python - python将字节转换为图像
- javascript - 使用 React JS 制作多页面 Web 应用
- c++ - 在 C++ 中创建文件夹
- sql-server - nvarchar 列查询到 varchar 列在 SQL Server 2017 中没有返回值
- c++ - cmake install "noconfig" 脚本有什么用?IE。“XXXTarget-noconfig.cmake”
- x86-64 - grub 可以切换到长模式吗?
- unix - 如何根据分隔符“|”删除尾随和前导空格 使用 tr 实用程序?
- visual-studio-2013 - 我没有使用实体框架代码优先方法创建的表出现错误。错误是无效的对象名称“XXXX”
- android - 无法在 Android Studio 上运行下载的项目
- javascript - 如何在没有用户输入的情况下创建 Microsoft Outlook Web 插件 (Javascript)?