Ok, so I'm trying to do this task I got some classes and first of all I want to make them and make their print functions. Later I'll need to make and "addStudent" function to the group class, so i want my array to be bigger than needed.
so here's my code
static void Main(string[] args)
{
Student test = new Student("as", "dsa");
Student test1 = new Student("Stola", "Stolova");
Student test2 = new Student("alo", "maloo");
Student[] students = new Student[5];
students[0] = test;
students[1] = test1;
students[2] = test2;
Group grp = new Group("aklas", students);
grp.print();
Console.Read();
}
//^
//this is my main function
class Student
{
private string firstName;
private string lastName;
public string FirstName
{
get
{
return firstName;
}
}
public Student()
{
firstName = null;
lastName = null;
}
public Student(string nameOne, string nameTwo)
{
firstName = nameOne;
lastName = nameTwo;
}
public void formatPrint()
{
Console.WriteLine("{0,10} {1,10}", firstName, lastName);
}
// ^this is one of the classes
class Group
{
private string name;
private Student[] students;
public Group()
{
name = null;
students = null;
}
public Group(string grpName, Student[] grpStudents)
{
name = grpName;
students = grpStudents;
}
public void print()
{
Console.WriteLine("{0, 15}", name);
for(int i = 0; i < students.Length; i ++)
{
if (students[i].FirstName != null)
students[i].formatPrint();
}
}
}
//^ and here is the other one.
So the problem is i got array declared for 5 elements and there are only 3 elements inside. everything works fine if i declare the array with 3 elements, but if they are 4 or more im getting that null exeption even that i made that if statement. So where is my mistake? Thank you!