E
E
Evgeny Semashko2020-10-01 21:33:44
C++ / C#
Evgeny Semashko, 2020-10-01 21:33:44

How to pass an array reference from one method to another?

There is such a code

namespace ConsoleApp39
{
    class Program
    {
        static void Main(string[] args)
        {
            Student[] students = new Student[3];

            Student student = new Student();
            
            

            for (int i = 0; i < students.Length; i++)
            {
                students[i].SetInfo();
            }

            student.GetInfo(students);
        }
    }

   struct Student
    {
       
        string LastName;
        string FirstName;
        int GroupNumber;
        int[] scores;

 
       public void SetInfo()
        {
            Console.WriteLine("Введите фамилию: ");
            LastName = Console.ReadLine();
            Console.WriteLine("Введите имя: ");
            FirstName = Console.ReadLine();
            Console.WriteLine("Номер группы: ");
            GroupNumber = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Выпишите оценки: ");
            scores = new int[5];

            int inp;
            for (int i = 0; i < scores.Length; i++)
            {
                inp = Convert.ToInt32(Console.ReadLine());
                scores[i] = inp;
            }
        }

        public void GetInfo(Student[] students)
        {
           
            for (int i = 0; i < scores.Length; i++)
            {
                if (scores[i] <= 2)
                {
                    Console.WriteLine($"Фамилия: {students[i].LastName}" +
                        $"\nИмя: {students[i].FirstName}" +
                        $"\nНомер группы: {students[i].GroupNumber}");
                }

            }
            Array.Sort(scores);
        }
    }
}

Here I get a null exception, since the method cannot get a reference to an array from another method . How can I pass a reference to an already filled array to another method?
for (int i = 0; i < scores.Length; i++)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-10-01
@evgenysemashko

You have the wrong data transmitted there, you seem to be confused

namespace ConsoleApp39
{
    class Program
    {
        static void Main(string[] args)
        {
            Student[] students = new Student[3];
            for (int i = 0; i < students.Length; i++) students[i].SetInfo();            
            Student.GetInfo(students);
        }
    }

    struct Student
    {

        string LastName;
        string FirstName;
        int GroupNumber;
        int[] scores;


        public void SetInfo()
        {
            Console.WriteLine("Last name: ");
            LastName = Console.ReadLine();
            Console.WriteLine("Name: ");
            FirstName = Console.ReadLine();
            
            Console.WriteLine("Number: ");
            while (!int.TryParse(Console.ReadLine(), out GroupNumber)) {
                Console.WriteLine("Only number allowed");
            };

            Console.WriteLine("Scores: ");
            scores = new int[5];

            int inp;
            for (int i = 0; i < scores.Length; i++)
            {                
                while (!int.TryParse(Console.ReadLine(), out inp))
                {
                    Console.WriteLine("Only number allowed");
                };
                scores[i] = inp;
            }
        }

        public static void GetInfo(Student[] students)
        {
            foreach (var student in students)
            {
                for (int i = 0; i < student.scores.Length; i++)
                {
                    if (student.scores[i] <= 2)
                    {
                        Console.WriteLine($"Last name: {student.LastName}\n name: {student.FirstName}\n Group: {student.GroupNumber}");                        
                    }

                }
            }
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question