How can I implement this complex object
-
I am the new about class and objects in c#. I just want to define an object for this issue after than I will save and restore from a file this ExamFile.
ExamFile.Title = "Test Exam File";
ExamFile.FileName = "my First Exam.mfe";ExamFile.Exam[0].ExamName = "This is my First Exam";
ExamFile.Exam[1].ExamName = "This is my Second Exam";ExamFile.Exam[0].Question[0].Remark = "First Exam's first Q";
ExamFile.Exam[0].Question[0].Type = "Multiple";
ExamFile.Exam[0].Question[1].Remark = "First Exam's Second Q"
ExamFile.Exam[0].Question[1].Type = "Single";ExamFile.Exam[1].Question[0].Remark = "Second Exam's First Q";
ExamFile.Exam[1].Question[0].Type = "Multiple";Thanks
-
I am the new about class and objects in c#. I just want to define an object for this issue after than I will save and restore from a file this ExamFile.
ExamFile.Title = "Test Exam File";
ExamFile.FileName = "my First Exam.mfe";ExamFile.Exam[0].ExamName = "This is my First Exam";
ExamFile.Exam[1].ExamName = "This is my Second Exam";ExamFile.Exam[0].Question[0].Remark = "First Exam's first Q";
ExamFile.Exam[0].Question[0].Type = "Multiple";
ExamFile.Exam[0].Question[1].Remark = "First Exam's Second Q"
ExamFile.Exam[0].Question[1].Type = "Single";ExamFile.Exam[1].Question[0].Remark = "Second Exam's First Q";
ExamFile.Exam[1].Question[0].Type = "Multiple";Thanks
Something like..
public class ExamFileBase
{
public string Title { get; set; }
public string FileName { get; set; }
public List Exam { get; set; };public ExamFileBase() { Title = "Default title"; FileName = "default\_filename.mfe"; Exam = new List(); }
}
public class ExamBase
{
public string ExamName { get; set; }
public List Question { get; set; }public ExamBase() { ExamName = "Default exam"; Question = new List(); }
}
public class QuestionBase
{
public string Remark { get; set; }
public string Type { get; set; }
}Only problem is, for the List properties, you'd have to initiate a class before accessing it. IE:
ExamFileBase ExamFile = new ExamFileBase();
ExamFile.Exam.Add(new ExamBase());Unless you made them static so each was initialized auto-matically.
-
I am the new about class and objects in c#. I just want to define an object for this issue after than I will save and restore from a file this ExamFile.
ExamFile.Title = "Test Exam File";
ExamFile.FileName = "my First Exam.mfe";ExamFile.Exam[0].ExamName = "This is my First Exam";
ExamFile.Exam[1].ExamName = "This is my Second Exam";ExamFile.Exam[0].Question[0].Remark = "First Exam's first Q";
ExamFile.Exam[0].Question[0].Type = "Multiple";
ExamFile.Exam[0].Question[1].Remark = "First Exam's Second Q"
ExamFile.Exam[0].Question[1].Type = "Single";ExamFile.Exam[1].Question[0].Remark = "Second Exam's First Q";
ExamFile.Exam[1].Question[0].Type = "Multiple";Thanks
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 Exams = new List();
}public class Exam : List
{
public string Name { get; set; }
readonly public List Questions = new List();
}public class Question
{
public string Remark { get; set; }
public string Type { get; set; }
}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.
-
I am the new about class and objects in c#. I just want to define an object for this issue after than I will save and restore from a file this ExamFile.
ExamFile.Title = "Test Exam File";
ExamFile.FileName = "my First Exam.mfe";ExamFile.Exam[0].ExamName = "This is my First Exam";
ExamFile.Exam[1].ExamName = "This is my Second Exam";ExamFile.Exam[0].Question[0].Remark = "First Exam's first Q";
ExamFile.Exam[0].Question[0].Type = "Multiple";
ExamFile.Exam[0].Question[1].Remark = "First Exam's Second Q"
ExamFile.Exam[0].Question[1].Type = "Single";ExamFile.Exam[1].Question[0].Remark = "Second Exam's First Q";
ExamFile.Exam[1].Question[0].Type = "Multiple";Thanks
Hello, You question is very large. I will try to answer you with a simple and short answer, and if you feel you need more explanations, feel free to post back here. You will need more than a single object to hold your information. For instance, you could use a ExamFile, Exam and Question objects. In the ExamFile object, you could have a List, and in Exam object you could have a List. Here is a concrete example of a data structure to hold your information :
using System;
using System.Collections.Generic;public class ExamFile
{
public String Title { get; set; }
public String FileName { get; set; }
public List Exams { get; set; }
}public class Exam
{
public String ExamName { get; set; }
public List Questions { get; set; }
}public class Question
{
public String Remark { get; set; }
public String Type { get; set; }
}______________________ Dominic Goulet FroggedSoft