Reputation: 597
I have to create a list as follows : name, roll no subjectno, subject type subject no, subject type
e.g.
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"
I am not sure how to do this in C#. I tried making a list of student info and then for subject no and subject type i tried to make a dictionary. I have written the code like this -
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);
}
}
Upvotes: 0
Views: 181
Reputation: 7825
You can create struct or class to contain student's info and than add it to Dictionary<string, StudInfo>
, where string
is student name as Key.
Class for storing student info:
public class StudInfo
{
public int RollNum;
public List<string> Activities = new List<string>();
public StudInfo(int num)
{
RollNum = num;
}
}
And its usage:
var dict = new Dictionary(string, StudInfo);
var info = new StudInfo(12);
dict.Add("Ellis", info);
Upvotes: 0
Reputation: 832
Here how I would do. First create these two class
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()
{
}
}
Then fill in the objects as follows :-
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);
Then iterate thru the Dictionary as follows:-
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 />");
}
}
Hope this helps.
Upvotes: 1
Reputation: 330
Your requirements aren't clear, but it looks best to create a Subject
class as well.
class Subject{
public int Id {get;set;}
public string Type {get;set;}
}
From there, you can change Dictionary<String, String> subjects
to a simple List<Subject>
type. And change addstudentinfo
method to take a Subject
as a parameter.
You can further improve it by ensuring that only one instance of a particular Subject
ever exists by modifying the Subject
class (For example, to use the Creator pattern).
Upvotes: 0