Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How do I name a list collection at run time?

If I want to populate names from a string array and make a list of each, i.e.

I have an array courseList[i]

If I want to populate each item form it and make a separate list of each. What approach Shall I use?

List<string> ______ = new List<string>();


How can I fill this ______ dynamically?
Posted
Updated 4-May-11 22:56pm
v2
Comments
Per Söderlund 5-May-11 4:04am    
Why would you want to use dynamic alias?
How would you call that list later on?
Can you put up some code example on what you are trying to do?
Syed Ammar Haider 5-May-11 5:36am    
After a user(student) has logged in.. This is to execute..
SqlDataReader dr;
string sSQLStudent = "select Course.course_code 'Course Code', Course.course_name 'Course Name' from Course, Time_Table where course.course_code = Time_Table.course_code and Time_Table.start_time <= (SELECT CONVERT(TIME,GETDATE()) AS HourMinuteSecond) and Time_Table.end_time >= (SELECT CONVERT(TIME,GETDATE())) and Time_Table.day = (SELECT DATENAME(DW, GETDATE())) and course.course_code in ( select course_code from Student_Course where user_id = '" + Session["sessionLoginUserName"].ToString().Trim() + "')";


SqlCommand cmd = new SqlCommand(sSQLStudent, conn);
dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dr.Close();

List<string> courses = new List<string>();
DataTableReader dtr = new DataTableReader(dt);
while(dtr.Read() != false)
{
string a = dtr.GetValue(0).ToString();
courses.Add(a);
}

foreach (string course in courses)
{
List<string> _______ = new List<string>();
}


Now here in ________, how can i add course code retrived from data table and made a separate list for each. Actually its the idea of a virtual class where i need to add students of particular class only. that is why i need to achieve this...
Dalek Dave 5-May-11 4:56am    
Edited for Grammar and Readability.

I don't think you can do it this way. You probably need to create your lists and add them to some collection class (Dictionary for example) that uses the name as the key.
 
Share this answer
 
Comments
Dalek Dave 5-May-11 4:57am    
Good answer.
Richard MacCutchan 5-May-11 5:08am    
Thanks - it was a guess!
Olivier Levrey 5-May-11 5:02am    
Good solution. Have a 5.
Richard MacCutchan 5-May-11 5:08am    
Thanks.
Just like the previous answers, I do not know what you try to accomplish but you could also implement your own named collection that just derives from a list of strings and add a name property to the class.

charp
public class NamedList : List<string>
{
  public string Name { get; set; }
}
</string>
 
Share this answer
 
Comments
Oshtri Deka 5-May-11 4:40am    
Quick & dirty! 5!
Olivier Levrey 5-May-11 5:01am    
It looks like the most reasonnable solution. My 5.
Sounds painful, though could certainly be done.

But why not use KeyValueCollection
Each element has key = name (the ______ that you want) - and value = the corresponding List<string>.
 
Share this answer
 
Comments
Olivier Levrey 5-May-11 5:02am    
Good solution. Have a 5.
If I got this right
List courses;
really is
List<string> courses;


and you need to store a student list within a list of courses?

//A class symbolizing a course
class course
{
//ReadOnly name of the current course
  private string _CourseName;
  public string CourseName{get{return _CourseName;}}
  
//List of students attending to the course.
  public List<string>students= new List<string>();

  public course(string CourseName)
  {
//Set name on current course when constructing it.
   this._CourseName = CourseName;
  }
}</string></string>


Use this class where you read the courses like this.
C#
List<course>courses = new List<course>();
while(dtr.Read() != false)
                  {
                      string a = dtr.GetValue(0).ToString();
                      //Add a course in the collection and name it with the value from string a above.
                      courses.Add(new course(a));
                  }
//now you can loop through all courses and get the list of students within it.
for(int i =0;i<courses.count;i++)>
   {
     //display course name
     Console.WriteLine(courses[i].CourseName);
     //get list of attending students for adding or reading.
     courses[i].students.add("New Student");
   }

</course></course>


I´m not sure this is what you want and I just wrote it by hand so it might contain errors.
Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900