Click here to Skip to main content
15,925,440 members
Home / Discussions / C#
   

C#

 
GeneralRe: re Code Pin
Paul Conrad29-Nov-08 13:48
professionalPaul Conrad29-Nov-08 13:48 
AnswerRe: re Code Pin
PIEBALDconsult29-Nov-08 15:12
mvePIEBALDconsult29-Nov-08 15:12 
GeneralRe: re Code Pin
Thomas Weller29-Nov-08 17:51
Thomas Weller29-Nov-08 17:51 
GeneralRe: re Code Pin
Paul Conrad30-Nov-08 6:38
professionalPaul Conrad30-Nov-08 6:38 
AnswerRe: re Code Pin
Christian Graus29-Nov-08 15:26
protectorChristian Graus29-Nov-08 15:26 
GeneralRe: re Code Pin
Thomas Weller29-Nov-08 17:57
Thomas Weller29-Nov-08 17:57 
GeneralRe: re Code Pin
Christian Graus29-Nov-08 18:13
protectorChristian Graus29-Nov-08 18:13 
GeneralRe: re Code Pin
Thomas Weller29-Nov-08 18:24
Thomas Weller29-Nov-08 18:24 
AnswerRe: re Code Pin
Thomas Weller29-Nov-08 18:07
Thomas Weller29-Nov-08 18:07 
GeneralRe: re Code Pin
Paul Conrad30-Nov-08 6:29
professionalPaul Conrad30-Nov-08 6:29 
AnswerRe: re Code Pin
MickCurley30-Nov-08 0:34
MickCurley30-Nov-08 0:34 
QuestionA problem with System.Drawing.Drawing2D Pin
Pedram Behroozi29-Nov-08 9:48
Pedram Behroozi29-Nov-08 9:48 
GeneralRe: A problem with System.Drawing.Drawing2D Pin
Luc Pattyn29-Nov-08 10:46
sitebuilderLuc Pattyn29-Nov-08 10:46 
GeneralRe: A problem with System.Drawing.Drawing2D Pin
Guffa29-Nov-08 20:44
Guffa29-Nov-08 20:44 
GeneralRe: A problem with System.Drawing.Drawing2D Pin
Pedram Behroozi30-Nov-08 8:29
Pedram Behroozi30-Nov-08 8:29 
GeneralRe: A problem with System.Drawing.Drawing2D Pin
Guffa30-Nov-08 9:10
Guffa30-Nov-08 9:10 
GeneralRe: A problem with System.Drawing.Drawing2D Pin
Pedram Behroozi30-Nov-08 9:49
Pedram Behroozi30-Nov-08 9:49 
QuestionGraphics ? Pin
nlowdon29-Nov-08 8:49
nlowdon29-Nov-08 8:49 
AnswerRe: Graphics ? Pin
Christian Graus29-Nov-08 13:47
protectorChristian Graus29-Nov-08 13:47 
GeneralRe: Graphics ? Pin
electriac30-Nov-08 6:44
electriac30-Nov-08 6:44 
QuestionHow can I implement this complex object Pin
mbudak29-Nov-08 1:16
mbudak29-Nov-08 1:16 
AnswerRe: How can I implement this complex object Pin
Chris Copeland29-Nov-08 3:19
mveChris Copeland29-Nov-08 3:19 
AnswerRe: How can I implement this complex object Pin
Dag Oystein Johansen29-Nov-08 4:08
Dag Oystein Johansen29-Nov-08 4:08 
From your code it's evident that you want ExamFile to be a collection of Exam objects. Exams have names and a collection of Question(s), and Question has two string attributes, Remark and Type.

This object model is easily expressed in C#:

public class ExamFile 
{
    public string Title { get; set; }
    public string Name { get; set; }
    readonly public List<exam> Exams = new List<exam>();
}

public class Exam : List<question>
{
    public string Name { get; set; }
    readonly public List<question> Questions = new List<question>();
}

public class Question
{
    public string Remark { get; set; }
    public string Type { get; set; }
}
</question></question></question></exam></exam>


You could construct the object graph and then set properties as in your posted code, like this:

ExamFile file = new ExamFile();
file.Add(new Exam());
file.Add(new Exam());
file.Exams[0].Questions.Add(new Question());
file.Exams[0].Questions.Add(new Question());
file.Exams[1].Questions.Add(new Question());
file.Exams[1].Questions.Add(new Question());


and then do as in your posted code

file.Title = "Test";
...
file.Exams[1].Questions[1].Remark = "tough question";


However, in most cases you'd create a question object and set it's properties before adding it to an exam. Also note that the types themselves should usually have constructors that take some parameters. This has two side-effects: Less code to write to use the type, which is not very important but a marginal benefit, and a guarantee that an instance of a type cannot be created without certain minimum information.

Types should typically be designed to be flexible and general. Coding convenience can be achieved by writing little "helper methods" separately from the type itself. In this case, you could do something like this:

void addQuestion(Exam e, string remark, string type)
{
   Question q = new Question();
   q.Remark = remark;
   q.Type = type;
   e.Questions.Add(q);
}

Exam addExam(ExamFile f, string name)
{
   Exam e = new Exam();
   e.Name = name;
   f.Exams.Add(e);
   return e;
}

ExamFile file = new ExamFile();
f.Title = "test";
f.Name = "my first exam.mfe";

Exam e = addExam(file, "First Exam");
addQuestion(e, "remark #1", "Mutiple");
addQuestion(e, "remark #2", "I don't know");


and so on.
AnswerRe: How can I implement this complex object Pin
Dominic Goulet1-Dec-08 1:00
Dominic Goulet1-Dec-08 1:00 
Questionaddin to IE Pin
Maverickcool29-Nov-08 1:06
Maverickcool29-Nov-08 1:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.