0

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);
            }
        }
    }
1
  • 1
    After you create the array in ReadStudents you should return it to the caller of that method. By not doing that you are discarding the work that method accomplished. The Print method should be called by something and instead of a counter it should probably have the Student instance passed to it but now we are at speculation and mater of opinion. Commented Apr 7, 2020 at 15:12

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried to return the array by adding ``` public static Student[] ReadStudents(string[] Lines) ``` are returning the value to ``` Stuedents = StudentReport.ReadStudents(lines); ``` But I still get an error of can't implicitly convert type to string[]
@AnasBaheh are you returning the studentArray or Lines, using the return keyword? I don't know without seeing your updated code.

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.