3

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

2

You could define a property that would hold the number of the students that attend your university and initialize it, while you create a university.

class University
{
    public int NumbersOfStudents { get; private set; }

    public Student[] students;

    public University(int numberOfStudents)
    {
        NumberOfStudents = numberOfStudents;
        students = new Student[numberOfStudents];
    }
}

So, when you would like to create a university with 1000 students, you do so like this:

var university = new University(1000);

Now the object called university has an array of students with a 1000 "slots". Actually, each item in this array is null. I mean

university.Students[0], university.Students[1], ...

are null.

However, I don't believe that this is the most effective way to achieve that you want. If I were you, I would have picked a List for this purpose. We usually use an array, when we know the number of items that we will place there and we will meet any case in the future that we would want to add one more item, this is impossible, using an array, because an array has a fixed size. On the other hand a List is a data structure with no fixed size, it can expand as you want to add more items in it.

In terms of code, I would opt this:

class University
{
    public List<Student> Students {get; private set; }

    public University()
    {
        Students = new List<Student>();
    }
}

Now you could create an instance of a university and doing so, you will create also a new list of students with no items.

var university = new University();

Now you could add a student to the student's of the university as simple as the following:

university.Students.Add(new Student());
Christos
  • 53,228
  • 8
  • 76
  • 108
  • It's not *impossible* to do it with an array, it is rather difficult, but can be done. – Der Kommissar May 22 '15 at 16:08
  • Of course it can be done. That I say is impossible is to alter the size of the array, once it have been created. I am sorry for any misconfusion. – Christos May 22 '15 at 16:20
  • You have it backwards. You need pass int numberOfStudents in the first and not the second. – paparazzo May 22 '15 at 16:27
  • @Blam oops...you are bloody correct...The daemon of copy paste :) Thank you very much ! – Christos May 22 '15 at 16:28
  • `University` could inherit `List` – Jodrell May 22 '15 at 16:28
  • @Christos thank for your answer. But when I modified the code Application error occurs. The name `NumberOfStudents` does not exist in the current context – seyyed mahdi mousavi May 22 '15 at 16:45
  • @mahdi313 have your tried the lastest vesrion? Because I had a naive error that Blam found. – Christos May 22 '15 at 16:47
  • @Christos is it worth mentioning it should probably be `IList` just to formalize his exposure to collection interfaces? Because ultimately we don't care if it's an array list, we just care it's a list we can insert into, whether that's a Linked-List, BST or otherwise, we just need to know the exposed functions of the data-structure – Jlalonde Jun 06 '19 at 12:43
1

I would refactor your code a little bit to use Lists, lists are more powerful than arrays in this case, because arrays aren't mutable.

static void Main(string[] args) {
 Console.WriteLine("Please enter the number of students: ");
 Count = int.Parse(Console.ReadLine());
 University university = new University(Count);
 university.AddStudent(new Student() /*Student logic here.*/);
 university.Sort();
 university.Report();
}

class Student {
 //Properties here...
 public Student(){
      //Default values here...
 }
}

class University {
 private List<Student> _Students;
 public Students {
      get {
           return _Students;
      }
      //In Microsoft's guidelines, list shouldn't be exposed as properties.
      protected set {
           //Add list replacement logic here.
           _Students = value;
      }
 }
 public University(Int32 StudentCount) {
      _Students = new List<Student>(new Student[StudentCount]);
 }
 public void AddStudent(Student StudentToAdd) {
      //Add your logic here...
      _Students.Add(StudentToAdd);
 }
}
Lino
  • 19,604
  • 6
  • 47
  • 65
kevinrodriguez-io
  • 1,054
  • 9
  • 15