Efficient way of updating items
-
Hi, I'd like to update an item from my collection. I used the code below but this calls the Dispose event which I'd like to avoid. this.CurrentUser.Companies[idx] = comp; So instead I'd like to use something like below which works and doesn't trigger the Dispose but seems inefficient with 4 calls made. Does that make sense? Can anybody please recommend a more efficient approach? private void UpdateCompanyFields(int idx, Company comp) { this.CurrentUser.Companies[idx].BusinessName = comp.BusinessName; this.CurrentUser.Companies[idx].Address.AddressOne = comp.Address.AddressOne; this.CurrentUser.Companies[idx].Address.AddressTwo = comp.Address.AddressTwo; this.CurrentUser.Companies[idx].Address.City = comp.Address.City; }
-
Hi, I'd like to update an item from my collection. I used the code below but this calls the Dispose event which I'd like to avoid. this.CurrentUser.Companies[idx] = comp; So instead I'd like to use something like below which works and doesn't trigger the Dispose but seems inefficient with 4 calls made. Does that make sense? Can anybody please recommend a more efficient approach? private void UpdateCompanyFields(int idx, Company comp) { this.CurrentUser.Companies[idx].BusinessName = comp.BusinessName; this.CurrentUser.Companies[idx].Address.AddressOne = comp.Address.AddressOne; this.CurrentUser.Companies[idx].Address.AddressTwo = comp.Address.AddressTwo; this.CurrentUser.Companies[idx].Address.City = comp.Address.City; }
You first line of code will not automatically "call the Dispose event" unless your Companies collection specifically does that when you overwrite an element - in which case there is probably a very, very good reason for it. And your second code does not do the same thing at all - it overwrites parts of the class instance held by the collection which alters the instance throughout the app. For example:
class MyClass
{
public int Value {get; set;}
public MyClass(int x) { Value = x;}
}
MyClass[] data = new MyClass[2];
MyClass x = new MyClass(333);
MyClass y = new MyClass(666);
myClass z = new MyClass(999);
myClass[0] = x;
myClass[1] = y;
myClass[0] = z;
myClass[1].Value = 111;Changing x.Value will not affect the value in MyClass[0] and vice versa, changing the value of y will affect the value of myClass[1] and vice versa. Changing myClass[0] or z will affect both because they are the same instance. I think you need to think a bit more carefully before you continue, and find out why the collection is calling Dispose - it implies the Company has important other info which needs to be replaced and Disposed, which your second code example does not do.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Hi, I'd like to update an item from my collection. I used the code below but this calls the Dispose event which I'd like to avoid. this.CurrentUser.Companies[idx] = comp; So instead I'd like to use something like below which works and doesn't trigger the Dispose but seems inefficient with 4 calls made. Does that make sense? Can anybody please recommend a more efficient approach? private void UpdateCompanyFields(int idx, Company comp) { this.CurrentUser.Companies[idx].BusinessName = comp.BusinessName; this.CurrentUser.Companies[idx].Address.AddressOne = comp.Address.AddressOne; this.CurrentUser.Companies[idx].Address.AddressTwo = comp.Address.AddressTwo; this.CurrentUser.Companies[idx].Address.City = comp.Address.City; }
The real inefficiency comes from the memory management in the second instance. With your original code there is an object in memory already and this line
this.CurrentUser.Companies[idx] = comp;
simply copies the memory address of "comp" to the Companies array at index idx. However this code;
this.CurrentUser.Companies[idx].BusinessName = comp.BusinessName;
Looks at the length of comp.BusinessName, reserves the same amount of memory elsewhere, copies the contents of BusinessName into that memory, then assigns that memory address to Companies[idx].BusinessName. All that memory management makes that operation slow, and you are doing it multiple times. There is nothing wrong with the dispose event being called, it doesn't make the code inefficient. Unnecessary memory management is more likely to be a cause of inefficiency.
-
Hi, I'd like to update an item from my collection. I used the code below but this calls the Dispose event which I'd like to avoid. this.CurrentUser.Companies[idx] = comp; So instead I'd like to use something like below which works and doesn't trigger the Dispose but seems inefficient with 4 calls made. Does that make sense? Can anybody please recommend a more efficient approach? private void UpdateCompanyFields(int idx, Company comp) { this.CurrentUser.Companies[idx].BusinessName = comp.BusinessName; this.CurrentUser.Companies[idx].Address.AddressOne = comp.Address.AddressOne; this.CurrentUser.Companies[idx].Address.AddressTwo = comp.Address.AddressTwo; this.CurrentUser.Companies[idx].Address.City = comp.Address.City; }
COBOL has a MOVE CORRESPONDING that accomplishes what you want. Internally, the compiler will still generate "4 moves". Why do you think that is? And what part is "disposable"? And how exactly is your code "calling Dispose"? Or, are you bluffing?
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal