Adding Lists to Lists

  • Thread starter Thread starter Fred Chateau
  • Start date Start date
F

Fred Chateau

I have three classes. Among other properties, these classes contain the
following lists:

----
public class QuestionsForm
{
public IList<SubCategories> SubCategoriesList { get; set; }
}

public class SubCategories
{
public IList<Questions> QuestionsList { get; set; }
}

public class Questions
{
public IList<ResponseOptions> ResponseOptionsList { get; set; }
}

----
QuestionsForm questionsForm = new QuestionsForm();

questionsForm.SubCategoriesList = new List<SubCategories>();

foreach (var subCategory in subCategories)
{
questionsForm.SubCategoriesList.Add(subCategory);

List<Questions> questionsList = new List<Questions>();

foreach (var question in questions)
{
questionsList.Add(question);
}

questionsForm.SubCategoriesList.Add(questionsList);
<--- Error
}
}

----
Error: Argument type "List<Questions>" is not assignable to parameter type
"SubCategories".

----
Would someone tell me how to handle this properly?

Regards,

Fred Chateau
 
I have three classes. Among other properties, these classes contain the
following lists:

----
public class QuestionsForm
{
public IList<SubCategories> SubCategoriesList { get; set; }
}

public class SubCategories
{
public IList<Questions> QuestionsList { get; set; }
}

public class Questions
{
public IList<ResponseOptions> ResponseOptionsList { get; set; }
}

----
QuestionsForm questionsForm = new QuestionsForm();

questionsForm.SubCategoriesList = new List<SubCategories>();

foreach (var subCategory in subCategories)
{
questionsForm.SubCategoriesList.Add(subCategory);

List<Questions> questionsList = new List<Questions>();

foreach (var question in questions)
{
questionsList.Add(question);
}

questionsForm.SubCategoriesList.Add(questionsList); <--- Error
}
}

Try:

questionsForm.SubCategoriesList.QuestionsList = questionsList;

or:

questionsForm.SubCategoriesList.QuestionsList.AddRange(questionsList);

depending on what you want.

Arne
 
Arne Vajhøj said:
Try:

questionsForm.SubCategoriesList.QuestionsList = questionsList;

or:

questionsForm.SubCategoriesList.QuestionsList.AddRange(questionsList);

That's what I thought too, but I don't seem to have that scope available.
There is no "QuestionsList" in Intellisense, following
"questionsForm.SubCategoriesList".

Regards,

Fred Chateau
 
Fred said:
I have three classes. Among other properties, these classes contain the
following lists:

----
public class QuestionsForm
{
public IList<SubCategories> SubCategoriesList { get; set; }
}

Note: while you haven't really explained these classes, from the context
it appears that the "SubCategories" and "Questions" classes should
really be named "SubCategory" and "Question", since a single instance of
each of those classes would represent only a single sub-category or
question, respectively.
public class SubCategories
{
public IList<Questions> QuestionsList { get; set; }
}

public class Questions
{
public IList<ResponseOptions> ResponseOptionsList { get; set; }
}

----
QuestionsForm questionsForm = new QuestionsForm();

questionsForm.SubCategoriesList = new List<SubCategories>();

foreach (var subCategory in subCategories)
{
questionsForm.SubCategoriesList.Add(subCategory);

List<Questions> questionsList = new List<Questions>();

foreach (var question in questions)
{
questionsList.Add(question);
}

At this point, you have created a list of questions. Presumably, you
want to add every question in the question list to the sub-category
currently being processed. Following the pattern of initialization
shown for the sub-category itself – specifically, you are initializing
the property, rather than adding to its current contents – I think the
line of code you want is this:

subCategory.QuestionsList = questionsList;

Alternatively, perhaps:

subCategory.QuestionsList.AddRange(questionsList);

Except that the type of QuestionsList is IList<Questions> and not
List<Questions>, so you don't actually have access to the AddRange()
method in that section of the code. So that line would have to be:

((List<Questions>)subCategory.QuestionsList).AddRange(questionsList);

…and it would throw an exception if for any reason that property ever
got initialized with an implementation of IList<Questions> other than
List<Questions>.

It's not clear where "subCategories" comes from, but presumably it's
initialized somewhere, and you appear to be copying the "SubCategories"
instances (which should be named "SubCategory instances" :) ) to a brand
new list in your QuestionsForm instance (i.e. "SubCategoriesList").

It's not clear why you're making a copy of the list, rather than just
assigning "subCategories" to the "SubCategoriesList" property of the
QuestionsForm, nor is it clear where the "questions" variable comes from
or how it's initialized, nor why the "subCategories" collection you
start with is not already initialized with each "SubCategories" instance
already having the necessary questions added to its list of questions.

But, ignoring all of those issues, it seems you want to initialize the
SubCategories instance with the current list of questions, so the first
statement I suggest above would do that.


Unfortunately, you have not provided anything that actually specifically
and precisely describes exactly what the output of the code should be.
Without that specification, we are forced to try to infer the intent
from code that does not work, which can be very difficult and
error-prone. If the above does not address your question, you really
should spend some time and write a precise description of what the code
should _do_.

Pete
 
Peter Duniho said:
Unfortunately, you have not provided anything that actually specifically
and precisely describes exactly what the output of the code should be.
Without that specification, we are forced to try to infer the intent from
code that does not work, which can be very difficult and error-prone. If
the above does not address your question, you really should spend some
time and write a precise description of what the code should _do_.

I thought it might be simpler to just show you the code, than to try to
explain it in detail.

BTW, I managed to get it working, but I still do not understand why I am
unable to reach "questionsForm.SubCategoriesList.QuestionsList" directly.

Regards,

Fred Chateau
 
Fred said:
[...]

BTW, I managed to get it working, but I still do not understand why I am
unable to reach "questionsForm.SubCategoriesList.QuestionsList" directly.

Hi Fred,

Looking at your original post, SubCategoriesList is of type
IList<SubCategories> which means a collection of SubCategories object.
In order to get to QuestionsList, you have to access the collection by
passing their index. For example,
questionsForm.SubCategoriesList[0].QuestionsList

Regards.
 
Fred said:
I thought it might be simpler to just show you the code, than to try to
explain it in detail.

From what you posted, it appears you are doing the complete
initialization of the original data structures in that code. Thus my
first suggestion is the one you want.
BTW, I managed to get it working, but I still do not understand why I am
unable to reach "questionsForm.SubCategoriesList.QuestionsList" directly.

Because there's no such property in the IList<SubCategrories> type,
which is the type of the SubCategoriesList property. As "kndg" says, if
you really want to get at a QuestionsList property from the
SubCategoriesList property, you _must_ identify a particular
SubCategories instance in the list and use that.

But, in reality that's not what you want. You _actually_ want to set
the QuestionsList for the _current_ SubCategories object you're working
on. That object is represented by the "subCategory" variable, and so
you can simply assign the new IList<Questions> like I showed you before.

Pete
 
That's what I thought too, but I don't seem to have that scope
available. There is no "QuestionsList" in Intellisense, following
"questionsForm.SubCategoriesList".

Then maybe:

questionsForm.SubCategoriesList[subcat].QuestionsList = questionsList;

or:

questionsForm.SubCategoriesList[subcat].QuestionsList.AddRange(questionsList);

Arne
 
Back
Top