|
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.
|
|
|
|
|
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<exam>, and in Exam object you could have a List<question>.
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<exam> Exams { get; set; }
}
public class Exam
{
public String ExamName { get; set; }
public List<question> Questions { get; set; }
}
public class Question
{
public String Remark { get; set; }
public String Type { get; set; }
}</question></exam>
|
|
|
|
|
I want to build a generic web analytic application using C#
I want to do it by making an addin to IE (and using windows hooks to catch all the user's events)
I tried to find some examples on how to build an addin to IE using C# but I didnt find anything
can someone please point me to where I can find some examples to get me started?
thanks
|
|
|
|
|
i have a c# executable which connects to an sql database and uses active directory for authentication. it runs fine from any folders as long as it's from any of my local drives but it silently doesn't do anything if i run from a network folder. anyone has an explanation for this? and how to fix it? thanks.
----------------------------------------------------------
"unzip; strip; touch; finger; mount; fsck; more; yes; unmount; sleep" - my daily unix command list
|
|
|
|
|
The reason is that Network drives are not automatically on trusted zone. One way to fix this is to increase assembly trust with .NET Framework configuration utility. Also you can sign the assembly and based on the signature add privileges etc.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
|
Even though this has changed in .NET 3.5 SP1, you may still want to read up on "Code Access Security" to understand why your code doesn't work from a network share.
|
|
|
|
|
Hello,
I want to capture the pc desktop screen when any event is fired like start menu is clicked or "my documets" is opened. So for that any listener is needed to listen continuous system events?
And how i recognize when any event is fired?
Help me if anyone knows.
|
|
|
|
|
Why have you asked the same thing three times ?
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hello,
I have made different queries, 1. how can i open start command using mouse coordinates, 2. is there any listener is needed to listen continuously and perform task when any event is fired like open c:drive.
So please help me out for that. It will be great for me.
|
|
|
|
|
Hello,
I had capture the windows screen of my pc, if i click on "start", the start ,menu should be opened.
Will you please help me for doing this?
|
|
|
|
|
Hello,
I have developed the basic screen capturing of the remote pc to my pc, now when i click on start menu from my pc i need to open start menu on that remote pc n my pc also. So i have the coordinate of start menu but for remote pc how can i use that coordinte?
Please help me out if anyone knows .
Thank you.
|
|
|
|
|
I want to research broadcast, how can I get the VNC open source?
|
|
|
|
|
Try Google.
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
|
Hello everyone,
I want to generate an XML file like below. My confusion is what technologies could I use to concatenate all information into a single XML document.
The root confusion is, I have a class called teacher which matches the teacher sub-section (ID, name and major) -- which I could use serialization technologies to generate teacher part XML information.
For other parts, like department, I also have a class to represent the information -- I could also use serialization technologies to serialize the information to XML -- but it has no relationship between teachers. So, my confusion is, how to concatenate different parts altogether, like department, club and teachers, and also make the whole XML document looks like below to reflect the relationships and hierarchies between them?
<?xml version="1.0" encoding="utf-8"?>
<Department>
<DepartmentInfo ID ="100">
<Location> San Jose </Location>
<Type> Computer Science</Type>
</DepartmentInfo>
<Clubs>
<Club ID="200">
<Name>Football</Name>
</Club>
<Club ID="201">
<Name>Basketball</Name>
</Club>
</Clubs>
<Teachers>
<Teacher ID="300">
<Name>Thomas</Name>
<Major>Algorithm</Major>
</Teacher>
<Teacher ID="301">
<Name>Zhou</Name>
<Major>Linear Programming</Major>
</Teacher>
</Teachers>
</Department>
thanks in advance,
George
|
|
|
|
|
|
Thanks Mika,
Do you have any samples or tutorials to recommend?
regards,
George
|
|
|
|
|
|
Thanks Mika!
I like them!
One more question, you option to my problem solution is to generate the XML document manually, not creating a class which contains all information and serialize the class?
regards,
George
|
|
|
|
|
If you only have to deal with XML files created by your application (no external ones), then I'd use XML serialization, it's just so comfortable to use
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks Greeeg,
I would also like to use serialization if I could. As I mentioned in the below URL, the issue which blocks me from using serialization is, the information are stored in different objects/classes, I do not know how to aggregate them altogether. Any ideas?
http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2824797[^]
regards,
George
|
|
|
|
|
I think you have at least three options:
1. Create the document fully manually by looping the objects you want and add the information to the document you need
2. First serialize the objects, then loop through the xml documents created by serialization, read them and copy the relevant nodes to a single document
3. Create a generic collection (or nested collections if you need) and add your objects into that collection. After that, serialize it
I guess that if you have strict requirements for the xml 'format', serialization may not produce automatically the format you need. Also you can implement ISerializable on your objects, but that may be a longer way.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Thanks Mika,
1.
So, in my situation, you think generate the XML document manually is the best way?
2.
One more question about performance, I heard serialization is always faster than writing XML document manually, is that true?
regards,
George
|
|
|
|
|
1. I can't say it's the best option of those I listed, it depends on (all of) your requirements.
2. This should be true since serialization is normally a process done by framework and it's optimized. When writing your own document, you have your own custom MSIL code that is executed and it can hardly compete when it comes to speed.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|