开发者

C# - Generic Lists - Can't Clear and Add all over Again

The Problem is .. I have a Set of Lists IDs .. Each one Related to Set of Traffic IDs in the Database ..

I wanna Use One List ID for each loop to get that set of TL IDs and store them in a List but in a Class-seem manner !

when I don't add TL_ids.Clear(); at beginning .. all results are added so the final result becomes = Count of all Traffic lights ids ..

and when I clear before Par.Clear(); all results came back with Zeros .

Any help ?

List<int> TL_ids = new List<int>();
List<string> TL_Desc = new List<string>();

//Array That will Hold Role ID.
ArrayList Par = new ArrayList();
//Get All Traffic Lights info. , but based on Iteration of Lists IDs.
for (int i = 0; i < List_IDs.Count; i++)
{
  Par.Clear开发者_StackOverflow社区();
  Par.Add(new SqlParameter("@List_id", List_IDs[i].ToString()));
  SqlDataReader Dr = GetTLs.ExecSPSelect_dr("GetTLsByListID", Par);

  while (Dr.Read())
  {
    //Fill Intermediate Lists.
    TL_ids.Add(int.Parse(Dr[0].ToString()));
    TL_Desc.Add(Dr[1].ToString());
  }

  ALL_TLsBy_Lists.Add(new Lists(List_IDs[i], TL_ids, TL_Desc));
}


Hope I got your question right.

Lists are passed by reference.

You would have to create a new List in each iteration when creating new Lists(...).

for (int i = 0; i < List_IDs.Count; i++)
{
  TL_ids = new List<int>();
  TL_Desc = new List<string>();
  ...
}

Otherwise, you end up with several Lists objects with the same TL_ids and TL_Desc!

So if you clear TL_Desc in the last iteration, you will get an empty list in all your Lists objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜