Thursday 15 July 2010

c# - How to convert Struct to a Class -



c# - How to convert Struct to a Class -

so i'm new c# , i've been tasked converting student_data struct class, lot of info found when looked online c++ didn't find useful. code works struggling modify fit task. console programme prints out values in array.

using system; using system.collections.generic; using system.linq; using system.text; namespace pupil { class programme { 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(" "); } } }

you need rename struct class. i'd recommend looking @ microsoft's style guides , naming conventions c#

c# struct

No comments:

Post a Comment