I have a global list of objects used to hold values, values along with the Ids will be parsed into the ObjectManager
function, the Ids can only ever be what is stated in the list. I want the function to update the value that corresponds to the Id
.
Initially I tried to use a foreach loop which wasn't updating the objects in the list. Using the below code does update the objects in the list, but it only checks for the id that's parsed in, I am now trying this with a switch statement, early days.
What is the best way of achieving what I'm trying to do?
Sample Code:
public List<Object> obj = new List<Object>
{
new Object { id = 4, val = 20 },
new Object { id = 1, val = 34 },
new Object { id = 16, val = 27 },
new Object { id = 9, val = 36 }
};
public void ObjectManager(List<Object> myobj, int id, int val)
{
int i = 0;
int j = 0;
while (i < myobj.Count)
{
if(myobj[i].id == id)
{
j = myobj[i].val;
myobj[i].val = j + val;
}
}
}