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.

7
  • 1
    Why not use a List<Student>()? Commented May 22, 2015 at 15:53
  • a. Student[] is an array, List<Student> is a list. b. var myArray = new Student[variableNameHere]; Commented May 22, 2015 at 15:54
  • you already have it in Cunt (surely a misspelling?) - now just add it a parameter to at least your constructor, put it into a field and there you go - but this is really basic stuff, so you might want to reread your introductions Commented May 22, 2015 at 15:54
  • Pass in the number of students into the University constructor? Commented May 22, 2015 at 15:55
  • erm, why not class University : List<Student> { } Commented May 22, 2015 at 16:27

2 Answers 2

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());
Sign up to request clarification or add additional context in comments.

8 Comments

It's not impossible to do it with an array, it is rather difficult, but can be done.
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.
You have it backwards. You need pass int numberOfStudents in the first and not the second.
@Blam oops...you are bloody correct...The daemon of copy paste :) Thank you very much !
University could inherit List<Student>
|
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);
 }
}

Comments

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.