I have a custom property called Questions the code is below.
public class Questions
{
private List<Question> _q = new List<Question>();
public List<Question> Question
{
get { return _q; }
}
}
public class Question
{
public string Text { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Answer { get; set; }
private List<string> _Options = new List<string>();
public List<string> Option {
get { return _Options; }
}
}
I then populate the property with the code below
XmlDocument doc = new XmlDocument();
Question q = new Question();
Questions qs = new Questions();
doc.Load(string.Format(@"questions.xml"));
XmlNodeList list = doc.SelectNodes("/questions/question");
foreach (XmlNode node in list)
{
q.Text = node.SelectSingleNode("text").InnerText;
q.Type = node.SelectSingleNode("type").InnerText;
q.Name = node.SelectSingleNode("name").InnerText;
XmlNodeList options = doc.SelectNodes("/questions/question/options");
foreach (XmlNode option in options)
{
q.Option.Add(option.SelectSingleNode("option").InnerText);
}
load.Visible = false;
qa.Visible = true;
qs.Question.Add(q);
DisplayQuestion(qs);
}
Now when I try to access it with the code below I do not get the output I expect. And thus this is where I need help. The Sample XML is at the bottom
Label1.Text = q.Question[CurrentQ].Text;
for (int i = 0; i < q.Question[CurrentQ].Option.Count; i++)
{
CheckBoxList1.Items.Add(q.Question[CurrentQ].Option[i]);
}
XML:
<?xml version="1.0"?>
<questions>
<question>
<num>1</num>
<type>radio</type>
<text>Do you like cake?</text>
<options>
<option>Yes</option>
<option>No</option>
<option>Sometimes</option>
</options>
<name>cake</name>
</question>
<question>
<num>2</num>
<type>dropdown</type>
<text>Do you like TV?</text>
<options>
<option>Yes</option>
<option>No</option>
<option>Sometimes</option>
</options>
<name>tv</name>
</question>
<question>
<num>3</num>
<type>checkbox</type>
<text>What do you like?</text>
<options>
<option>Cake</option>
<option>TV</option>
<option>Flipper Reruns</option>
</options>
<name>flipper</name>
</question>
</questions>
And the output:
What do you like?
Yes
Yes
Cake
Yes
Yes
Cake
Yes
Yes
Cake
The answer to this is fairly simple.
the error is in the piece of code below.
C# automatically works by reference ( meaning pointer to an object instead of copying it).
So in the above statement you declare
Question q = new Question();then you fill it and add it to your list and fill it again. this means only one Question is instantiated and therefor the result is all the same.This way everytime a new question gets instantiated and added to the list instead of overwritten. Because in your questionList every entry is only a pointer to the one instance of the question object you have.
Oh and why did i put the
DisplayQuestion(qs);down there?Because you dont want to draw the entire list over and over again do we?
make sure your Displayquestion function lops trough each question and draws the right controls
Kind Regard Roxas
I hope this helps