Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Efficient way of updating items

Efficient way of updating items

Scheduled Pinned Locked Moved C#
databasequestionannouncement
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Danpeking
    wrote on last edited by
    #1

    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; }

    OriginalGriffO F L 3 Replies Last reply
    0
    • D Danpeking

      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; }

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • D Danpeking

        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; }

        F Offline
        F Offline
        F ES Sitecore
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Danpeking

          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; }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups