Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have an Array list with an Id and a value in it. I need to access the value in the ArrayList so that I can insert a new record in to the database any help is very much apreciated. Here's my code The _newListOfIds is The Arraylist
C#
if (_newListOfIds.Count > 0)
                   {
                       for (int i = 0; i < _newListOfIds.Count; i++)
                       {
                           short iNewPermissionGroupId = Convert.ToInt16(_newListOfIds[i]);
                           objPermission.PermissionGroupId = iNewPermissionGroupId;
                           objPermission.UserId = userId;
                           objPermission.Deleted = false;
                           objPermission.LastUpdateDate = dtCurrent;
                           objPermission.LastUpdateUserId = userId;
                           objPermission.CreateDate = dtCurrent;
                           objPermission.CreateUserId = userId;
                           objPermission.JoinTransaction(objTrans);
                           objPermission.Insert();
                       }
                   }
Posted
Updated 17-Oct-11 6:24am
v2
Comments
[no name] 17-Oct-11 12:58pm    
"I have an Array list with an Id and a value in it."
Please clarify. Are this two separate lists?
frostcox 17-Oct-11 13:12pm    
The array list is populated from a list box with an Id and description. I just need to access the Id. I know its prob just something but it's been a long day.;)

1 solution

Hi,
if you'd like to access the value in arraylist by index, just do it as in any regular array:

C#
var value = newListOfIds[index];

Anyway I would recommend to iterate over the values in array by using foreach constructs, as accessing over an index is more expensive in terms of running time.

C#
foreach(var value in _newListOfIds){
/*operate with value variable*/
}

Regards
 
Share this answer
 
Comments
frostcox 17-Oct-11 13:17pm    
Thanks very much

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