0

I have started learning c# just over 3 months so for homework I have an exercise where i have to read from a text file with 50 names and marks. I have to find the name with the highest mark display it and save the result to a text file.This is what I have come up with.

namespace FINAL
{
    class Program
    {


       public  static string [] parts;

        struct StudentDetails
        {

            public string name; 
            public string surname; 
        };
        struct CourseWorkResults
        { public int mark;
        };
       static void Main(string[] args)
       {  

           string [] Lines = System.IO.File.ReadAllLines(@"F:\FINAL\StudentExamMarks.txt");
            foreach (string line in Lines)
           {
               string[] parts = line.Split();
            }
           StudentDetails[] Student = new StudentDetails[50];
            CourseWorkResults[] Results = new CourseWorkResults[50];


            GetName( ref Student ,ref parts );
            FindMark ( ref Results , parts);
            FindMax (ref Results);

        }
        static void GetName(ref StudentDetails[]Student,  ref string[] parts)
        {
            for (int i = 0; i < Student.Length; i++ )
            {
                Student[i].name = parts[0];
                Student[i].surname = parts[1];
            }
        }
       static void FindMark ( ref CourseWorkResults[]Results, string [] parts )
        {
            for (int i=0; i< Results.Length;i++)
            {
                Results[i].mark = Convert.ToInt16(parts[2]);
            }

        }
       static void FindMax( ref CourseWorkResults[]Results)
        {
            for (int i = 0; i < Results.Length; i++)
            {
                int max = 0;
                max = Results[2].mark;
                if (Results[i].mark > max)
                {
                    max = Results[i].mark;
                }
            }
        }
       static void DisplayResults( ref CourseWorkResults[]Results, StudentDetails[]Student, int max)

           {

        Console.WriteLine(" The student with the highest score is {0} {1} :{2}", Student[0].name, Student[1].surname, max);
           } 
   }
 }

My problem is the GetName procedure , I can't pass by reference parts. What am I doing wrong?

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73

1 Answers1

3

The problem here is that a struct is an immutable value type. So when you write :

 Student[i].name = parts[0];

You are just trying to modify a local a copy of Student[i]. Actually, each time you trying to access Student[i], it will always return a copied value instead the instance on the list.

A workaround is to have StudentDetails as class instead of struct

  class StudentDetails
  {
      public string name; 
      public string surname; 
  };

The issue applies also for CourseWorkResults struct.

In addition, as arrays are already reference in .NET. You don't really need to pass it as reference unless you want it to reference another object. I.e, this :

    static void GetName(StudentDetails[]Student,  ref string[] parts)
    {
        for (int i = 0; i < Student.Length; i++ )
        {
            Student[i].name = parts[0];
            Student[i].surname = parts[1];
        }
    }

will work as

    static void GetName(ref StudentDetails[]Student,  ref string[] parts)
    {
        for (int i = 0; i < Student.Length; i++ )
        {
            Student[i].name = parts[0];
            Student[i].surname = parts[1];
        }
    }

Unless you add something like this:

    static void GetName(ref StudentDetails[]Student,  ref string[] parts)
    {
        Student = anotherStudentArray ; // Here the ref param is really needed as you want to reassign Student to a new array instance

        for (int i = 0; i < Student.Length; i++ )
        {
            Student[i].name = parts[0];
            Student[i].surname = parts[1];
        }
    }
Perfect28
  • 11,089
  • 3
  • 25
  • 45
  • To add to this answer I would suggest reading up on POCO's https://msdn.microsoft.com/library/dd456872(v=vs.100).aspx and use the POCO as a data store. – Paul Dec 08 '15 at 12:00