Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
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.
Posted
Updated 23-Apr-16 19:49pm
v2
Comments
Dave Kreskowiak 23-Apr-16 16:11pm    
Did you want to tell us on which line this message occurs or do we just guess at it?
m.justice10 23-Apr-16 16:18pm    
I am so sorry, any line that contains
field_current_student_array_index
myArray_StudentGradeDec
myArray_StudentNameStr

All of these are underlined red. If you want every line for them I can provide but with a quick look you should see them.
m.justice10 23-Apr-16 16:20pm    
Also one second, I just noticed not all my code posted so editing it now.
Sergey Alexandrovich Kryukov 23-Apr-16 16:28pm    
Sorry, we cannot deal with non-questions here.
But I have a question for you: which part of this error message is unclear to you?
It's possible that the term "extension method" confuses you, but then read on it's; this is a whole separate topic.

"Changing the name"..? How? Changing some name is only consistent when you change it... well, consistently everywhere. By the way, do you use or know Visual Studio refactoring engine?

—SA
m.justice10 23-Apr-16 16:50pm    
Let me explain my problem, this is a boring code for a Intro to C# class I am taking. I do not have the mindset for this and every assignment has been headache after headache, the 3 lines I put in the comments with the field_current, and the myArrays are the lines that give this code and I simply have no idea why. I have no idea what Visual Studio refractoring engine is, Sorry I took time out of your day, I'll continuing research this for hours on end.

1 solution

Start by looking at exactly what you are doing here:
C#
{
    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)

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:
C#
Car carOriginalGriff = new Car("Red");
Car carMJustice10 = new Car("Green");
Car carMike = new Car("Blue");
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":
C#
public class Car
   {
   private string colour;
...
   public void ShowColour()
      {
      Console.WriteLine(this.colour);
      }
...
   }
And when you call the method it works for each instance:
C#
Car carOriginalGriff = new Car("Red");
Car carMJustice10 = new Car("Green");
Car carMike = new Car("Blue");
carOriginalGriff.ShowColour();
carMJustice10.ShowColour();
carMike.ShowColour();
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:
C#
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?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900