Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot access List<> within a List<>
    text
    copied!<p>Generics rookie struggling to access a list inside a class which is contained in a list. Basically I am trying to list the occupations of people that the user entered (I am sure there is an easier way of doing this, but this code was made to practise generics).</p> <p><strong>Class Code</strong></p> <pre><code>namespace GenericPersonClass { class GenericPerson&lt;T&gt; { static public List&lt;T&gt; Occupations = new List&lt;T&gt;(); string name, famName; int age; public GenericPerson(string nameS,string famNameS,int ageI, T Note) { name = nameS; famName = famNameS; age = ageI; Occupations.Add(Note); } public override string ToString() { return "The name is " + name + " " + famName + " and they are " + age; } } } </code></pre> <p><strong>Main Code</strong></p> <pre><code>namespace GenericPersonClass { class Program { static void Main(string[] args) { string token=null; string nameS, lastNameS,occS; int age; List&lt;GenericPerson&lt;string&gt;&gt; Workers = new List&lt;GenericPerson&lt;string&gt;&gt;(); while (token != "no" || token != "No") { Console.WriteLine("Please enter the first name of the person to input"); nameS = Console.ReadLine(); Console.WriteLine("Please enter the last name of the person " + nameS); lastNameS = Console.ReadLine(); Console.WriteLine("How old is " + nameS + " " + lastNameS); age = int.Parse(Console.ReadLine()); Console.WriteLine("What is the occupation of " + nameS + " " + lastNameS); occS = Console.ReadLine(); Console.WriteLine("Enter more data?...Yes/No"); Workers.Add(new GenericPerson&lt;string&gt;(nameS, lastNameS, age, occS)); token = Console.ReadLine(); } Console.WriteLine("You provide the following employment...\n"); for (int i = 0; i &lt; Workers.Count; ++i) { Console.WriteLine("{0} \n", Workers[0].Occupations[i]); //This line above is shown as wrong by VS2010, and intellisense does not see Occupations... } } } } </code></pre> <p>Thanks for any help, Leo</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload