3

I would like to say this was working and now it is not. I don't know what would have changed.

        var inquiry = repo.GetByInstanceId(2);

        foreach (var s in inquiry.Prop)
        {
            s.Prop = "Test 1 2 3...";
        }

        Assert.AreEqual(true, inquiry.S.Single().IsDirty, "S Should Be Dirty");

What is happening is before the Property object would get updated and everything would be grand. Now the Property is being updated within the foreach loop but after the foreach loop it's like I didn't do anything.

--EDIT-- Okay, I thinks IsDirty is throwing everyone off. I wanted to put the exact code, minus a few name changes. But IsDirty is not important here.

A basic representation of what I am doing is this:

        class test { public int check { get; set; } }

        var x = new List<test>();
        x.Add(new test(){ check = 1});

        foreach (var y in x)
        {
            y.check = 5;
        }

the Question is "Shouldn't this work?" I think it should, if not why? I had this working and I don't know what I have changed. I could show you my objects but I would have to modify the names so much it would be pointless. IsDirty isn't the problem. The value I am changing is changed inside the loop but not changed outside the loop.

14
  • 2
    welcome to stackoverflow. Its really hard to know what you are asking without the context of your application. Commented Apr 4, 2011 at 18:51
  • We need to see the code that sets the Prop and IsDirty properties. Commented Apr 4, 2011 at 18:53
  • I have updated the question. I hope that is better Commented Apr 4, 2011 at 19:08
  • 1
    In the foreach loop y is a local variable that is assigned a value from the List setting y to a different value does not change the list. However that is not the same as your original post where a property is set. In short we need to see the code for Prop and IsDirty. Commented Apr 4, 2011 at 19:10
  • what is the difference between a y and a property, should the property work?? I need to and was modifying the item from inquiry within the foreach loop using the iterator Commented Apr 4, 2011 at 19:12

2 Answers 2

7

In your added explanation you create a copy in y and modify the copy.

This happens when the type of the list items is a ValueType

Sign up to request clarification or add additional context in comments.

8 Comments

okay, but what if it is an instance of a class? that is what it really would be
If it is a ReferenceType then it should work. Really, if you keep on changing the information and examples we keep on shooting at a moving target. Do not ask us to answer questions about code we can't see.
I am trying only to refine the examples to give a more accurate picture of what I am trying to do. I will make one more edit and that will be the last one.
@user, the second example is not like the first and I removed it. The second example is illegal, you cannot modify the loop variable. The first example is modifying a property of the loop variable, which is fine as long as it's a class and not a struct.
Some advice for the next time you ask a question: it is ok to give a simpler example but make sure it compiles and has the SAME bug.
|
1

I think your problem might be involved with GetByInstanceId. If that is returning an IEnumerable then it is likely that your foreach is running through the values and making the change, but then your call to Single is reiterating and losing your changes. You might want to do a ToList after GetByInstanceId. Of course this is base on the assumption that inquiry.S.Single().IsDirty is a typo and should be inquiry.Single().IsDirty. Otherwise we need more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.