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)
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");
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();
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?