Anas Baheh
Anas Baheh

Reputation: 137

How to access an array of objects from another class

I'm trying to loop through an array of objects and print their properties from a different class.

My main class is

class Program
    {

        static void Main()
        {
            //This to be change to relative path
            string Path = @"C:\Users\";
            string[] lines = { }; ;

            //Reading file
            if (File.Exists(Path))
            {
                lines = File.ReadAllLines(Path);
                StudentReport.ReadStudents(lines);
            }
            else
            {
                Console.WriteLine("The file does't exist");
            }

            //Printing Students
            PrintStudent.Print(lines.Length);
        }
    }

I'm using this code to declare the array

public class StudentReport
    {
        public static void ReadStudents(string[] Lines)
        {
            //declare an array with the number of students
            Student[] studentArray = new Student[Lines.Length];
            int StudentCounter = 0;

            foreach (string Line in Lines)
            {

                String[] Student = Line.Split(',');

                //Calculating values
                string ID = Student[0].PadLeft(10, '0');
                string name = Student[1];

                //Initialize the object
                studentArray[StudentCounter] = new Student
                {
                    FullName = name,
                    ID = ID,
                };

                StudentCounter++;
            }
        }
    }

And I'm using this class to construct my student object

class Student
    {
        public string FullName { get; set; }
        public string ID { get; set; }
    }

To output the student object properties, I made another class. The problem is that I couldn't access the value of the objects array from my new class.

The class I made for outputting purposes is the following, but I cannot get the values. The error is 'Student does not contain a definition for student array

public class PrintStudent
    {
        public static void Print(int StudentCounter)
        {

            for(int i = 0; i > StudentCounter; i++)
            {
                Console.WriteLine(Student.studentArray[i].FullName);
            }
        }
    }

Upvotes: 0

Views: 1675

Answers (1)

Slate
Slate

Reputation: 3694

Your error is Student does not contain a definition for studentArray. This is because your Student class does not have studentArray, only the properties FullName and ID. So accessing Student.studentArray[i] doesn't make sense.

Probably what you want is for ReadStudents to return the studentArray so it doesn't go out of scope by changing the method signature to return the Student[], and calling return studentArray at the end.

Then, you can pass your studentArray to your PrintStudent.Print method in the parameters.

By the way, the for(int i = 0; i > StudentCounter; i++) has a wrong < and will never run (lines.Length which is the StudentCounter will always be >= 0)

You can use studentArray.Length, or a foreach loop to iterate over this array, rather than pass the StudentCounter.

Upvotes: 2

Related Questions