如何将Struct转换为类

时间:2014-11-06 04:56:25

标签: c# struct

所以我对C#很新,我的任务是将我的 student_data 结构转换为一个类,我在网上看到的很多信息都与C ++有关。我觉得没用。代码按原样工作,但我正在努力修改它以适应我的任务。它是一个控制台程序,可以打印出数组中的所有值。

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(" ");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您只需将其从struct重命名为class即可。我还建议查看Microsoft的样式指南和C#的命名约定

答案 1 :(得分:0)

试试这段代码,我认为你必须花一些时间阅读面向对象的编程。 有不同的实例化CLASS的方法。我写了一个简单的方法,以便你能理解。

另请阅读好文章并试用样品。 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();
    }
  }
}