2014-11-06 44 views
0

因此,我对C#相当陌生,并且我的任务是将我的student_data结构转换为类,我在网上查找时发现的很多信息都与C++,我没有发现有用的。代码的工作原理是,但我努力修改它以适应我的任务。它是一个控制台程序,它打印出数组中的所有值。如何将Struct转换为类

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

为什么你需要这样做? – mtahmed 2014-11-06 05:03:00

+0

这是我班的一部分。 – JamesGoodwin 2014-11-06 05:15:32

回答

0

你只需将其重命名从structclass。我也建议看一下微软的C#风格指南和命名约定。

+0

我试过了,它给了我一个错误“输出参数'学生'必须分配给控制权之前离开当前的方法”在线: '静态无效populateStruct(出student_data学生,字符串fname,字符串姓,int id_number,string ptitle,string pcode)' 和“使用未分配的参数'学生'”在线 'student.forename = fname;' 感谢您的帮助。 – JamesGoodwin 2014-11-06 05:07:28

+0

@JamesGoodwin在分配给它之前,你需要创建一个'new student_data()'。我强烈建议尽可能将班级重新命名为“学生”。在分配给它之前,你需要调用'new Student()'然后'student.Forname =“...”;' – John 2014-11-06 17:17:35

0

试试这段代码,我想你得花一些时间阅读面向对象编程。 有不同的方式来实例化CLASS.I写了简单的,以便你能理解。

也请阅读好的文章,并试用样品。 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(); 
    } 
    } 
} 
相关问题