2011-08-24 55 views
0

我有如下創建一個列表: 名稱,卷沒有 subjectno,主題型 主題無關,主題型我們可以用C#中的列表創建一個字典嗎?

例如

name[0] = ron, roll no[0] = 12 
     subjectno[0]=1, subject type[0]="english" 
     subjectno[1]=12, subject type[1]="maths" 

name[1] = elis, roll no[1] = 11 
     subjectno[0]=1, subject type[0]="english" 
     subjectno[1]=12, subject type[1]="maths" 
     subjectno[2]=14, subject type[2]="physics" 

我不知道如何在C#中做到這一點。 我試着製作一份學生信息清單,然後是主題號和主題類型,我試着製作一本字典。 我已經寫了這樣的代碼 -

class Student 
{ 


    public class StudentInfo 
    { 
     public String name { get; set; } 
     public int rollno { get; set; } 
     Dictionary<String, String> subjects; 
     public StudentInfo(String name, int rollno) 
     { 
      this.name = name; 
      this.rollno = rollno; 
     } 
     public void addstudentinfo(string subjectno, string subjecttype) 
     { 
      if (subjects == null) 
       subjects = new Dictionary<string, string>(); 

      subjects.Add(subjectno, subjecttype); 

     } 
    } 
+1

你必須定義你的對象是什麼C#類? –

+0

作業?請標記爲這樣。 –

回答

1

在這裏,我會怎麼做。首先創建這兩個類

public class Student 
{ 
    public int RollNo { get; set; } 
    public string Name { get; set; } 
    public Student(int rNo, string name) 
    { 
     this.RollNo = rNo; 
     this.Name = name; 
    } 
    public Student() 
    { 
    } 
} 

public class Subject 
{ 
    public int SubjectNo { get; set; } 
    public string Type { get; set; } 
    public Subject(int sNo, string sType) 
    { 
     this.SubjectNo = sNo; 
     this.Type = sType; 
    } 
    public Subject() 
    { 
    } 
} 

然後在對象填寫如下: -

Dictionary<Student, List<Subject>> studentLists = new Dictionary<Student, List<Subject>>(); 
    Student std = new Student() { RollNo = 11, Name = "John" }; 
    List<Subject> sbj = new List<Subject>() {new Subject(020, "Math"),new Subject(030,"English") }; 
    studentLists.Add(std, sbj); 

然後通字典迭代如下: -

StringBuilder sb = new StringBuilder(); 
    foreach (KeyValuePair<Student, List<Subject>> item in studentLists) 
    { 
     sb.Append("Student No : " + item.Key.RollNo + "<br />"); 
     sb.Append("Student Name : " + item.Key.Name + "<br />"); 

     foreach (var subjects in item.Value) 
     { 
      sb.Append("Subject No : " + subjects.SubjectNo + "<br />"); 
      sb.Append("Subject Name : " + subjects.Type + "<br />"); 
     } 
    } 

希望這有助於。

+0

非常感謝! – CPDS

0

您可以創建結構或類包含學生的信息,比其添加到Dictionary<string, StudInfo>,其中string是學生的名字作爲重點。

類存儲學生信息:

public class StudInfo 
{ 
    public int RollNum; 
    public List<string> Activities = new List<string>(); 

    public StudInfo(int num) 
    { 
     RollNum = num; 
    } 

} 

及其用法:

var dict = new Dictionary(string, StudInfo); 
var info = new StudInfo(12); 
dict.Add("Ellis", info); 
0

你的要求不明確,但它看起來最好創建一個Subject類爲好。

class Subject{ 
    public int Id {get;set;} 
    public string Type {get;set;} 
} 

從那裏,你可以改變Dictionary<String, String> subjects一個簡單List<Subject>類型。並更改addstudentinfo方法以Subject作爲參數。

您還可以通過確保只有一個特定的Subject的情況下不斷通過修改Subject類存在改進(例如,使用Creator pattern)。

相關問題