0

I have written a program that is supposed to get a list of names and information. Well, I got to solve this question a few classes, such as students and university have to answer. But the problem is where I plan to get students information

static void Main(string[] args) {

    Console.WriteLine("Some students enter the information you want? ");
    Count = int.Parse(Console.ReadLine());

    University university = new University();        
    university.Add();
    university.Sort();
    university.Report();
    Console.ReadKey();
}

To solve this problem, I have to specify the number of students .

class University { Student[] student = new Student[3];

 private int n = 0;
 public University()
 {

         for (int i = 0; i < 3; i++)
             student[i] = new Student();
 }

 public void Add()
 {
     Console.WriteLine();
     for (int i = 0; i < 3; i++)
     {
         student[n].Read();
         n++;
     }
 } 

For example, the number of students I have three. How do I create a way to enter the number of students who give information and do not default؟Instead of the number 3 is a variable that give the user the desired number.

Then change the program with help from friends, but I got this problem.

class University
{
 //  Student[] student = new Student[3];
   public int NumbersOfStudents { get; private set; }

  public Student[] student;

   public University(int numberOfStudents)
    {
        for (int i = 0; i < numberOfStudents; i++)
            student[i] = new Student();
    }
     private int n = 0;
     /* public University()
      {

              for (int i = 0; i < 3; i++)
                  student[i] = new Student();
      }*/

But when I run it I get the error: Object reference not set to an instance of an object.

notic:The program can not I use the list.

1
  • 1
    Please put more effort into formatting your questions in future... Commented May 23, 2015 at 13:22

1 Answer 1

2

You're never initializing your student field, so it's null. You could use:

// Note: public fields are almost *always* a bad idea
private readonly Student[] students;

public University(int numberOfStudents)
{
    students = new Student[numberOfStudents];
    for (int i = 0; i < numberOfStudents; i++)
    {
        students[i] = new Student();
    }
}

However, that's a pretty nasty solution, in my view - why are you creating all these useless Student objects? Why are you populating the University ahead of time?

It would be better to use List<T> rather than an array:

public class University
{
    private readonly IList<Student> students = new List<Student>();
    // Presumably some other fields and properties

    public void AddStudent(Student student)
    {
        students.Add(student);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

thanks Jon The program will work correctly as far as the code public void Add() { Console.WriteLine(); for (int i = 0; i < 3; i++) { student[n].Read(); n++; } } What should I put in place 3 to work properly?
@hojat: Well it won't blow up - unless numberOfStudents is less than 3 but that's a pretty odd design, IMO. Why would you read the details of exactly 3 students?
I know my students instead of 3, but I do not give access
@hojat: Do you mean students.Length? That's how you find out the length of an array. I still think it's a weird design though.
When I count the number of students in the variable inputs have specified the number of rings to suit the needs of my students. In the previous example, the number 3. With your help, I was able to determine variable. I do not know how much to put in the ring is because of error. public void Add() { Console.WriteLine(); for (int i = 0; i < 3; i++) { student[n].Read(); n++; } }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.