CSLA.NET Reflexion and Lists.

CSLA.NET Reflexion and Lists.

Old forum URL: forums.lhotka.net/forums/t/7930.aspx


Eblanchette posted on Monday, November 02, 2009

Hello everyone,

I am having problems with this bit of code. It seems that every element in my EditableRootList gets changed as soon as I add a new one in it.(all the elements take the value of the most recently added one). I hope someone can help.

for (int i = 2; i {
DataRow[] arrRows;
arrRows = new DataRow[2];
arrRows[0] = dtFromTo.RowsIdea [I];
arrRows[1] = dtFromTo.Rows[i + 1];
float division = (float)200.0 / (float)dtFromTo.Rows.Count;
float multiplication = division * i;
float division2 = multiplication / 2;
mainFrm.progressBar1.Value = ((int)division2 / 2);
DoEvents();
//CableEC newCable = null;
CableEC newCable = CableEC.NewCableEC();
object objToRef = (object)newCable;
objToRef = (CableEC)Utils.BOFiller.FillBusinessObject(ref objToRef, arrRows, true); //THIS LINE changes the value of everything in my list
lstCables.Add((CableEC)objToRef);

}

RockfordLhotka replied on Monday, November 02, 2009

Not too put too fine a point on it, but that looks like some seriously overcomplicated code, probably designed by someone with a less than solid understanding of how object references work. (hopefully not you :) - and in any case I don't mean to be insulting)

If I were designing this FillBusinessObject() method, I'd make it work like this:

CableEC newCable = CableEC.NewCableEC();
Utils.BOFiller.FillBusinessObject(newCable, arrRows, true);
lstCables.add(newCable);

When using an object reference (and newCable is an object reference) you don't need the ref keyword. And why have FillBusinessObject() return an object? It seems like it should clearly be filling the object I passed into it (newCable). Finally, .NET will automatically cast anything to type object, you shouldn't have to manually do a cast like that - certainly not for a scenario as simple as this.

Of course none of that explains why your whole list is getting changed by FillBusinessObject(), but my first assumption is that your FillBusinessObject() method is somehow doing a bad thing.

Copyright (c) Marimer LLC