-2

I am trying to use the input given in Form1 to define the size of the array but am getting an error saying that a field initializer cannot use a non-static field. This always worked for me in CLI but for some reason it is not working here.

    namespace Class_Grade_Register
    {
        public partial class Student_Input : Form
        {
            int number_Of_Students = 0;
            int counter = 0;

            string[] studentName = new string[number_Of_Students];
            int[] sfcGrade = new int[number_Of_Students]; // this is where I am getting the error. number_Of_Students is being underlined in red.
            int[] csGrade = new int[number_Of_Students];
            int[] sdtGrade = new int[number_Of_Students];
            int[] ddoocpGrade = new int[number_Of_Students];


            public Student_Input()
            {
                InitializeComponent();

            }

            public void Set_Number_Of_Students(int value)
            {
                number_Of_Students = value;
            }
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Anna D
  • 1
  • Do you expect that, say, `sfcGrade` is going to be resized when `number_Of_Students` is changed? – Heretic Monkey Jun 13 '19 at 21:08
  • Yes, that is correct. Basically, in the first form, I have a textbox in which the user has to enter the number of students. I am passing that value to Form2 and trying to use that to set the size of the arrays. – Anna D Jun 13 '19 at 21:11
  • 4
    A common pattern for new programmers is to create multiple arrays - one for each bit of data. A _better_ approach is to create a `Student` class with multiple properties - `Name`, `Grade` etc. Then use an array or `List`. – mjwills Jun 13 '19 at 21:30

3 Answers3

1

The idiomatic way of representing complex data (basically, anything where you have to connect two integers or strings together somehow) is by creating classes. Beginners are strangely reluctant to do this, instead creating arrays or multiple variables like student1Name, student2Name and so on.

Don't be like that, classes are a fundamental concept in object-oriented programming and you have got to become comfortable with them. Start with something like this:

public class Student
{
    public string Name { get; }
    public int Grade { get; }

    public Student(string name, int grade)
    {
        Name = name ?? throw new ArgumentNullException(nameof(name));
        Grade = (grade > 0) ? grade : throw new ArgumentException(nameof(grade));
    }
}

Add new properties as you need them. Also get in the habit of throwing exceptions when you detect something is wrong. Beginners have a bad habit of trying to hide exceptions. Doing that puts errors in your data and prevents you from finding them.

Now you can start creating students:

var me = new Student("Dour High Arch", 10);

If you want to create a bunch of students, use a list or collection:

var students = new List<Student>().
students.Add(me);

You have another bad design in your code; asking for the total number of students before adding them. This causes problems like null references if not as many students get added as your total number, or out-of-range exceptions if more students get added than your total number. Moreover in many real-world situations you won't know how many students or whatever there are until you've added them all. Use a design more like this:

while (true)
{
    var nextStudent = GetStudentFromSomewhere();
    if (nextStudent == null)
        break;

    students.Add(nextStudent);
}

Arrays are designed for collections which must always contain a set number of elements and cannot change, like the months of a year. If you expect the number of elements to change you should not use an array.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
  • Thanks for this. Yes, as you have realised, I am new to all this. I know that ideally I would use classes for this and that is what I would do usually however this exercise was given to us by the lecturer and he wants us to use an array. – Anna D Jun 14 '19 at 06:24
0

try making number_Of_Students static, something like this:

static int number_Of_Students = 0;

and also you need to modify your Set_Number_Of_Students method to something like this:

    public void Set_Number_Of_Students(int value)
    {
             number_Of_Students = value;
             studentName = new string[number_Of_Students];
             sfcGrade = new int[number_Of_Students]; 
             csGrade = new int[number_Of_Students];
             sdtGrade = new int[number_Of_Students];
             ddoocpGrade = new int[number_Of_Students];
    }
MorenajeRD
  • 849
  • 1
  • 11
  • 27
  • Thank you! I will give it a try now. – Anna D Jun 13 '19 at 21:14
  • Perfect! It worked! Thank you very much! On to the next problem :D – Anna D Jun 13 '19 at 21:16
  • Nice, @AnnaD please select me as the answer please – MorenajeRD Jun 13 '19 at 21:19
  • Actually, I am not sure it worked. I have a feeling that now it is setting the size of the array to 0. Because when I run the program, I am getting an Index Out Of Range Exception. – Anna D Jun 13 '19 at 21:40
  • for (counter = 0; counter < number_Of_Students; counter++) //Will allow the user to enter the details for the selected number of students in the class { for (int index = 0; index < number_Of_Students; index++) { lbl_StudentCounter.Text = "Please enter the information for Student " + (index + 2); sfcGrade[index] = Convert.ToInt32(txt_SFCInput.Text); }// End of for loop – Anna D Jun 13 '19 at 21:42
0
    int number_of_students = 0;

    int[] sfcGrade;

    public void Set_Number_of_Students(int value)
    {
        number_of_students = value;
        sfcGrade = new int[number_of_students];
    }

As described here the compiler will not necessarily initialize number_of_students before sfcGrade leading to the index size of sfcGrade being an undefined int.

  • Why do you need the `number_of_students = value;` line? It's redundant, you can simply use `value`? – Sach Jun 13 '19 at 23:38
  • You don't but I was trying to adapt their code while changing as little of the structure as possible to make it easier to understand what changes needed to be made to fix their error. – Will Jackson Jun 14 '19 at 11:13