Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, May 5, 2011 3:49 AM
how to name a list collection at run time?
like 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><string> ______ = new List<string><string>(); </string></string>
<string><string>How can I fill this ______ dynamically?</string></string>
All replies (12)
Thursday, May 5, 2011 4:29 AM ✅Answered
You might find a dictionary rather than a list more appropriate for your needs.
Thursday, May 5, 2011 5:46 AM ✅Answered
ohh.. for this thing you dont need to take new variable for each course....
List<string> oCourseList = new List<string>();
foreach (string course in courses)
{
oCourseList.Add(course);
}
//Another way... also you can take dictionalry variable and fill that course name with relatvant ID (if required)
Dictionary<int,string> oCourseList = new List<int, string>();
foreach (string course in courses)
{
oCourseList.Add(course);
}
Thursday, May 5, 2011 4:01 AM
You cannot. The name of a variable is an arbitrary thing that you choose at design time.
Thursday, May 5, 2011 4:07 AM
Thanks,
Is there any alternative to get it done at runtime? any alternative approach?
Thursday, May 5, 2011 4:20 AM
This is not possible...
But I am not understanding what you want to achive with this kind of logic.... Can you provide some more details like what you want to accomplish with this logic... then people can provide any alternate way...
Thursday, May 5, 2011 5:35 AM
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...
Thursday, May 5, 2011 5:52 AM
that is fine, but the problem relies in this part of code
foreach (string course in courses)
{
List<string> _______ = new List<string>();
}
how to make a list of each added variable in previously defined list. that means to fill the ______ dynamically at run time.??? hope you understand my question
Thursday, May 5, 2011 6:02 AM
That is not possible. This is the wrong way to implemenation because with this approach you can not achive any thing... becuase in c# you can assing dymica vaiable name because however you need to use that variable in your code then how can you use this variable in your code.
If you want to presever multiple list .. tthen eitehr take two dimattion array or use take one list as main and inseide that list you can add more than one list.....
List<List<string>> oList = new List<List<string>>();
foreach (string course in courseList)
{
List<string> ocourse = new List<string>();
oList.Add(ocourse);
}
Thursday, May 5, 2011 4:33 PM
that is fine, but the problem relies in this part of code
foreach (string course in courses)
{
List<string> _______ = new List<string>();
}
how to make a list of each added variable in previously defined list. that means to fill the ______ dynamically at run time.??? hope you understand my question
As already pointed out several times, that's not possible. And another thing, if you declare a variable inside a foreach-loop, that variable will be "destroyed" when you leave the foreach-loop, due to scoping rules.
Thursday, May 5, 2011 6:07 PM
Think about it for a little while. Assume that you could do what you wanted to do. Maybe
foreach (string course in courses) {
List<string> courses[i].Name = new List<string>(); // Not possible
}
What do you do next? How would you use these 'variables' that you have created? Outside the loop they no longer exist! They have gone out of scope.
Even if you could achieve what you wanted to achieve it wouldn't be any use.
Friday, May 6, 2011 12:21 AM
That is not a problem dear. we can make it a public property and put in something like cache, session etc..
Anyways thanks all. I got the solution i.e. Dictionary. Thanks to amitpatel.it particularly :)
Friday, May 6, 2011 2:46 AM
It is really good if you can mark as answer so other people not keep answering on it....