0

So I'm fairly new to C# and I've been tasked with converting my student_data struct to a class, a lot of the information I found when I looked online is to do with C++ which I didn't find useful. The code works as is but I am struggling to modify it to fit my task. It is a console program which prints out all the values in the array.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Student
{
    class Program
    {
        public struct student_data
        {
            public string forename;
            public string surname;
            public int id_number;
            public float averageGrade;
            public string ptitle;
            public string pcode;
        }

        public struct module_data
        {
            public string mcode;
            public string mtitle;
            public int mark;
        }

        static void populateStruct(out student_data student, string fname, string surname, int id_number, string ptitle, string pcode)
        {
            student.forename = fname;
            student.surname = surname;
            student.id_number = id_number;
            student.averageGrade = 0.0F;
            student.ptitle = ptitle;
            student.pcode = pcode;
        }

        static void populateModule(out module_data module, string mcode, string mname, int score)
        {
            module.mcode = mcode;
            module.mtitle = mname;
            module.mark = score;
        }

        static void Main(string[] args)
        {
            student_data[] students = new student_data[4];
            populateStruct (out students[0], "Mark", "Anderson", 1, "Title1", "Code1");
            populateStruct (out students[1], "John", "Smith", 2, "Title2", "Code2");
            populateStruct (out students[2], "Tom", "Jones", 3, "Title3", "Code3");
            populateStruct (out students[3], "Ewan", "Evans", 4, "Title4", "Code4");
            module_data[] modules = new module_data[4];
            populateModule (out modules [0], "MCode1", "MTitle1", 1);
            populateModule (out modules [1], "MCode2", "MTitle2", 2);
            populateModule (out modules [2], "MCode3", "MTitle3", 3);
            populateModule (out modules [3], "MCode4", "MTitle4", 4);

            foreach (student_data value in students) {
                printStudent(value);
            }

            foreach (module_data value in modules) {
                printModule(value);
            }

        }

        static void printStudent(student_data student)
        {
            Console.WriteLine("Name: " + student.forename + " " + student.surname);
            Console.WriteLine("Id: " + student.id_number);
            Console.WriteLine("Av grade: " + student.averageGrade);
            Console.WriteLine("Programme Title: " + student.ptitle);
            Console.WriteLine("Programme Code: " + student.pcode);
            Console.WriteLine(" ");
        }

        static void printModule(module_data module)
        {
            Console.WriteLine("Module Code: " + module.mcode);
            Console.WriteLine("Module Title: " + module.mtitle);
            Console.WriteLine("Module Mark: " + module.mark);
            Console.WriteLine(" ");
        }
    }
}
JamesGoodwin
  • 1
  • 1
  • 2

2 Answers2

0

You just need to rename it from struct to class. I'd also recommend looking at Microsoft's style guides and naming conventions for C#

John
  • 6,503
  • 3
  • 37
  • 58
  • I tried that and it gave me an error "The out parameter 'student' must be assigned to before the control leaves the current method" on the line: `static void populateStruct(out student_data student, string fname, string surname, int id_number, string ptitle, string pcode)` and "Use of unassigned out parameter 'student'" on the line `student.forename = fname;` Thanks for your help. – JamesGoodwin Nov 06 '14 at 05:07
  • @JamesGoodwin you need to create a `new student_data()` before you assign to it. I'd heavily recommend renaming the class to `Student` first though. right before you assign to it you need to then call `new Student()` then `student.Forname = "...";` – John Nov 06 '14 at 17:17
0

Try this code, I think you have to spend some time reading Object oriented programming. There are different way to instantiate the CLASS.I have written the simple one so that u can understand.

Also please read good articles and try out samples. Classes (C# Programming Guide)

  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Threading.Tasks;

  namespace ConsoleApplication1
  {
  class Program
   {
    public class student_data
    {
        public string forename;
        public string surname;
        public int id_number;
        public float averageGrade;
        public string ptitle;
        public string pcode;
    }

    public class module_data
    {
        public string mcode;
        public string mtitle;
        public int mark;
    }



    static void printStudent(student_data student)
    {
        Console.WriteLine("Name: " + student.forename + " " + student.surname);
        Console.WriteLine("Id: " + student.id_number);
        Console.WriteLine("Av grade: " + student.averageGrade);
        Console.WriteLine("Programme Title: " + student.ptitle);
        Console.WriteLine("Programme Code: " + student.pcode);
        Console.WriteLine(" ");
    }

    static void printModule(module_data module)
    {
        Console.WriteLine("Module Code: " + module.mcode);
        Console.WriteLine("Module Title: " + module.mtitle);
        Console.WriteLine("Module Mark: " + module.mark);
        Console.WriteLine(" ");
    }
    static void Main(string[] args)
    {
        List<student_data> students = new List<student_data>();
        student_data student = new student_data();
        student.forename = "Mark";
        student.surname = "Anderson";
        student.id_number = 1;
        student.ptitle = "Title1";
        student.pcode = "Code1";

        students.Add(student);

        List<module_data> modules = new List<module_data>();
        module_data module = new module_data();
        module.mcode = "MCode1";
        module.mtitle = "MTitle1";
        module.mark = 10;

        modules.Add(module);

        foreach (student_data value in students)
        {
            printStudent(value);
        }

        foreach (module_data value in modules)
        {
            printModule(value);
        }
        Console.ReadLine();
    }
  }
}
iShareHappyCode
  • 255
  • 2
  • 7
  • 22