" A"不包含“b”的定义并且没有扩展方法“C”。 [英] "A" does not contain a definiton for "b" and no extension method "C"

查看:81
本文介绍了" A"不包含“b”的定义并且没有扩展方法“C”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧我不知道这里发生了什么,我似乎无法理解为什么这不起作用。

Alright I have no idea what is going on here, I can't seem to see why this will not work.

        {
            const int CONST_array_max_size = 5;
            int field_current_student_array_index = 0;
            decimal[] myArray_StudentGradeDec = new decimal[5];
            string[] myArray_StudentNameStr = new string[5];
            string text = "";
            decimal result = 0M;
            if (this.textBox_StudentName.Text == "")
            {
                MessageBox.Show("Error 101: Invalid Student Name");
            }
            else
            {
                text = this.textBox_StudentName.Text;
                if (!(decimal.TryParse(this.textBox_CourseGrade.Text, out result) && (result <= 100M)))
                {
                    MessageBox.Show("Error 201: Invalid Grade Number");
                }
                else
                {
                    if (this.field_current_student_array_index >= 5)
                    {
                        MessageBox.Show("Array is full, cannot add any more to the Array...");
                    }
                    else
                    {
                        this.myArray_StudentNameStr[this.field_current_student_array_index] = text;
                        this.myArray_StudentGradeDec[this.field_current_student_array_index] = result;
                        MessageBox.Show("OK, added the student to the Array at Array index [" + this.field_current_student_array_index + "]");
                        this.myMETHODClearStudentInfo();
                    }
                    this.field_current_student_array_index++;
                }
            }
        }

        private void myBtnShowArray_Click(object sender, EventArgs e)
        {
            string str = "";
            decimal Grade = 0M;
            decimal Total = 0M;
            str = "STUDENT DATA ENTERED SO FAR:\n";
            for (int i = 0; i < this.myArray_StudentNameStr.Length; i++)
            {
                str = string.Concat(new object[] { str, "Array[", i, "]  ==>  Student #", i + 1 });
                if (this.myArray_StudentGradeDec[i] == 0M)
                {
                    str = str + "    (NOT ENTERED)";
                }
                else
                {
                    str = string.Concat(new object[] { str, "    Name:", this.myArray_StudentNameStr[i], "    Grade:", this.myArray_StudentGradeDec[i] });
                    d = decimal.op_Increment(Total);
                    Grade += this.myArray_StudentGradeDec[i];
                }
                str = str + "\n";
            }
            if (Total > 0M)
            {
                str = string.Concat(new object[] { str, "TOTAL STUDENTS:", Total, "    AVERAGE GRADE:", Grade / Total, "\n" });
            }
            this.label_AllStudentsInfo.Text = str;
        }

        private void myMETHODClearArrayInfo()
        {
            this.label_AllStudentsInfo.Text = "";
            this.field_current_student_array_index = 0;
            this.myArray_StudentGradeDec = new decimal[5];
            this.myArray_StudentNameStr = new string[5];
        }

        private void myMETHODClearStudentInfo()
        {
            this.textBox_StudentName.Text = "";
            this.textBox_CourseGrade.Text = "";
        }

        private void button_Clear_Click(object sender, EventArgs e)
        {
            this.myMETHODClearArrayInfo();
            this.myMETHODClearStudentInfo();
        }
    }
}





我尝试了什么:



更改我正在使用的名称就是我尝试的所有。



What I have tried:

Changing the name I am using is all that I have tried.

推荐答案

首先查看你到底在做什么:

Start by looking at exactly what you are doing here:
{
    const int CONST_array_max_size = 5;
    int field_current_student_array_index = 0;
...
    if (this.textBox_StudentName.Text == "")
    {
        MessageBox.Show("Error 101: Invalid Student Name");
    }
    else
    {
        text = this.textBox_StudentName.Text;
        if (!(decimal.TryParse(this.textBox_CourseGrade.Text, out result) && (result <= 100M)))
        {
            MessageBox.Show("Error 201: Invalid Grade Number");
        }
        else
        {
            if (this.field_current_student_array_index >= 5)



您在方法中声明 field_current_student_array_index 作为整数变量 - 尽管您没有显示方法标题,但它在里面一个'{'...'}'块,它包含可执行代码,所以它必须在一个方法中,因为代码永远不会在C#之外。

这样的声明意味着什么该变量是方法的本地变量:它是在方法执行开始时创建的,当方法退出时(通过到达方法的关闭'}'或执行 return 声明。

但是当你使用它时,你总是在前面添加这个 - 这是一个特殊名称,意思是这个insta nce。实例是什么可以非常简单:它是你宣布方法的类的一个例子。这可能有点令人困惑,但想想一辆车:你有一辆绿色汽车,我有一辆红色汽车,迈克有一辆蓝色的车。这些都是班级汽车的例子 - 我们可以通过英语中的特殊词语来引用它们:我的车,你的车,麦克斯的车,他的车,那车......最重要的是这个汽车,根据我们当时所处的情况,可以参考其中任何一个。

C#类是同样的:如果你定义了一个Car类,你可以很容易地说:


You declare field_current_student_array_index as a integer variable inside your method - though you don't show the method header, it's inside a '{'...'}' block and it contains executable code, so it has to be within a method as code can never be outside one in C#.
What a declaration like this means is that the variable is local to the method: it is created when execution of the method begins, and it is destroyed when the method exits (either by reaching the method's closing '}' or by executing a return statement.
But when you use it, you always prefix it with this - which is a "special name" meaning "this instance". And what an instance is can be pretty simple: it's an example of the class you declared the method in. That's probably a bit confusing, but think about a car: you have a green car, I have a red car, Mike has a blue car. these are all instances of the class "car" - and we can refer to them via special words in english: "my car", "your car", "mikes's car", "his car", "that car" ... and most importantly "this car" which can refer to any one of them depending on which we are sitting in at the time.
C# classes are the same: if you defined a Car class, you could easily say:

Car carOriginalGriff = new Car("Red");
Car carMJustice10 = new Car("Green");
Car carMike = new Car("Blue");

并且它们都作为Car类的单独实例存在。

在Car类中声明的每个方法中,你可以参考this car:

And they all exist as separate instances of the Car class.
Within each method you declare inside the Car class, you can refer to "this car":

public class Car
   {
   private string colour;
...
   public void ShowColour()
      {
      Console.WriteLine(this.colour);
      }
...
   }

当你调用方法时,它适用于每个实例:

And when you call the method it works for each instance:

Car carOriginalGriff = new Car("Red");
Car carMJustice10 = new Car("Green");
Car carMike = new Car("Blue");
carOriginalGriff.ShowColour();
carMJustice10.ShowColour();
carMike.ShowColour();

将在控制台上依次打印每种颜色。

(事实上,你不需要使用这个在那段代码中,因为你的意思是哪个 color 变量没有混淆 - 只有一级的一级)

但是在你的代码中,你使用 this 并尝试访问你声明为方法本地的变量,就像它们是类级别一样:

Will print each colour in turn on the console.
(in fact, you don't need to use this in that code, because there is no confusion as to "which" colour variable you mean - there is only the class level one)
But in your code, you use this and try to access variables that you have declared as local to the method as if they were class level:

this.field_current_student_array_index >= 5

并且编译器告诉您类中没有声明该名称的字段,属性或方法 - 消息这有点复杂,因为它覆盖了你还没有遇到的一些语言功能的所有基础。



所以...要么停止使用这个在变量前面(好主意),将变量移动到通过在方法之外声明它们的类级别(好主意,如果你希望它们可用于其他方法),或者两者兼而有(可能是最好的主意!)



这样做有道理吗?

And the compiler is telling you "There is no such field, property, or method with that name declared in the class" - the message is a bit more complicated that that, because it's covering all the bases to do with some language features you haven't met yet.

So...either stop using this in front of your variables (good idea), move your variables to class level by declaring them outside the method (good idea if you want them to be available to other methods), or both (probably the best idea!)

Does that make some sense?


这篇关于&QUOT; A&QUOT;不包含“b”的定义并且没有扩展方法“C”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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