2

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; 
        }
    }
}
SelrekJohn
  • 476
  • 2
  • 6
  • 21
  • Foreach makes the collection as readonly. So you can update the collection using foreach loop – Karthik Jul 27 '15 at 09:38
  • 1
    @Turbulence , you mean to say can not update the collection using foreach loop. – Kryptonian Jul 27 '15 at 09:40
  • @learningNew - Not in this case. I meant to say that Foreach loop makes the collecting as readonly – Karthik Jul 27 '15 at 09:43
  • I can't understand what your actual question is, are you asking how to do this with a switch statement? – Sayse Jul 27 '15 at 09:43
  • why j = myobj[i].val; myobj[i].val = j + val; but not myobj[i].val = myobj[i].val + val ? – HungPV Jul 27 '15 at 09:44
  • Apologies if I wasn't clear - I am asking the best way to do this - whether that be with a loop, switch or whatever, I just want to understand a best practise way (not a bodge) that will solve my issue. – SelrekJohn Jul 27 '15 at 09:45
  • The best way would be opinion based, you can use 3dd's approach (providing ID'S are unique) – Sayse Jul 27 '15 at 09:49

2 Answers2

3

You can select the item that you want to update using FirstOrDefault()

var variable = myobj.FirstOrDefault(o=>o.id = "YOUR_ID"

FirstOrDefault() returns null if the item is not found, so we need to check for null before updating the item

if (variable != null) variable.val = YOUR_VALUE

3dd
  • 2,520
  • 13
  • 20
  • Cool - seems good, shall give it a try, this negates using any loops. Should also update any property Id – SelrekJohn Jul 27 '15 at 09:49
  • 1
    @SelrekJohn: It doesn't negate loops, `FirstOrDefault` is basically a loop that examines each item using the predicate provided and stops when it finds something. If avoiding loops is important (eg because your collection is large and you do this a lot) then this is not the solution for you. – Chris Jul 27 '15 at 09:52
  • Sorry, poorly written, yes it's a loop still, but what I was doing (wrongly) was a while loop, then a foreach loop etc. – SelrekJohn Jul 27 '15 at 09:54
  • As @Chris says this is still a loop, just an elegant way of writing them using a predicate. If you're concerned about iterating the loop too many times a `Dictionary` will be a better option – 3dd Jul 27 '15 at 09:54
3

You should use Dictionary for this.

You'll find many solutions related add or update in Dictionary. like this or this

Community
  • 1
  • 1
Tirthak Shah
  • 515
  • 2
  • 11
  • Yes usually, however I kept this simple, there will be more properties, I just wanted a simple example. – SelrekJohn Jul 27 '15 at 09:48
  • 1
    Please have a look at Dictionary, you can have Object as value parameter as well, so you can design Dictionary as per you need – Tirthak Shah Jul 27 '15 at 09:50
  • 2
    If you ware wanting to find objects by some kind of id then a dictionary is exactly what you want. If you have multiple properties per id then create a property object (which it sounds like you already have) and store that against each id. – Chris Jul 27 '15 at 09:53