views:

38

answers:

1

Hello fellows, I am trying hard to update a business object inside a List<>.

I am using a checkbox list

  1. When a checkbox list item is clicked
  2. I check if it exists, if yes I mark it as Dirty (means the user unchecks an item)
  3. If not, I add a new item to the list (means the user clicks a new item) and store it in view state

    How do I update the Generic List with the Dirty Object ? On form update, do I foreach and make separate lists of dirty and new objects to send to DB layer or would you recommend any other way ?

Here is my code, sorry about the bad logic I am just a starter =(

protected void cblExclusions_SelectedIndexChanged(object sender, EventArgs e)
{
   if (cblExclusions.SelectedIndex != -1)
    {
        ftExclusions myExclusion=new ftExclusions();  // Business object
        ftExclusionsList myExclusionList=new ftExclusionsList();   // Business Obj. List
        int excId = Convert.ToInt32(cblExclusions.SelectedValue.ToString()); // value of checkbox list item which is clicked
        ftExclusionsList tempList = (ftExclusionsList)ViewState["ftExclusionList"];

        ftExclusions isExist =tempList.Find(delegate(ftExclusions tmpExclusion)
            {
                return (tmpExclusion.excluId == excId);
            });

        if (isExist != null)
        {
            isExist.isDirtyExclusion(); // Mark as dirty
            tempList.  // stuck here I don't grasp how to save this back to the list 
        }
        else
        {
            myExclusion = new ftExclusions();
            myExclusion.excluId = excId;
            myExclusion.fTrtID = Convert.ToInt32(lblTreatyNo.Text);
            myExclusion.ftExcluId = -1;     //new record
            myExclusion.isNewExclusion();   // Mark as new
            tempList.Add(myExclusion);
        }
        ViewState["ftExclusionList"] = tempList;                

    }
}
+1  A: 

You do no need to save it back to the list. Assuming ftExclusions is class and not struct, it is always passed by reference. Meaning that both tempList and isExist contain reference to the same object. And any changes on the object will be visible from both places.

Alex Reitbort
Thank you Alex. Yep it is a class (Dumb Business object). Edit... Ok, you were spot on about the reference being visible from both places. Part of my problem is solved.How would you advice on extracting IsDirty and IsNew from the List ? With a foreach or any other separate way ?
Popo
List<ftExclusions> exclusions = tempList.FindAll(e => e.IsDirty || e.IsNew);
devnull
@Alex not that it makes a differrence to your conclusion but arguments are in general passed by value. For reference type a _reference_ is passed by _value_ the object is not passed by ref (unless explicitly specified)
Rune FS
@rune fs: saying that reference to object is passed by value or that object is passed by reference is just a semantics. It means the same thing.
Alex Reitbort
@Alex there's an important difference between the two. let's say we have two methods Foo(object x) and Bar(ref object x). In the first (reference passed by value) if you do x = new object() that's invisible from outside the method where as if you write the same in Bar (where the argument is passed by reference) that will have effect outside the method as well (changing the value of the variable passed in as argument) so in this case not really relevant but there is important differences bewteen the two
Rune FS
@Rune I understand the difference between passing value type by value vs. by reference. However i said that "there is no difference between reference to object is passed by value or object is passed by reference". Of course they both different from reference to object is passed by reference.
Alex Reitbort
@Alex and that's exact what I disagree with. An object passed by reference in the term anyone with a C++ background would use to describe the functionality you get from the ref keyword and there not being strict stating that a reference to the object is passed by value can be misleading which a plura of questions here on SO proves
Rune FS
Hi Alex, I have marked it as answer as problem is solved. What do you say about the other fellow's points ?
Popo
@Popo: devnull gave you a way to get list of dirty and new objects
Alex Reitbort
Thank you Alex.
Popo