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. Delphi
  4. [SOLVED] Not Updating TObjectDictionary Value Correctly

[SOLVED] Not Updating TObjectDictionary Value Correctly

Scheduled Pinned Locked Moved Delphi
helpquestionannouncement
2 Posts 2 Posters 5 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.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    Hello guys.. I am having this TObjectDictionary object. I can successfully read and write values in this dictionary. But problem comes when I try to update one of my values. When I access a value, change and update it, then it stores some odd object whose values are changed automatically. Here is what I am trying.

    /////////////////////////////////////// storing this type based on 'StudentdId'
    type TStudent = class
    public
    StudentName, StudentClass : string;
    StudentId : Integer;
    end;

    ///////////////////////////////////////// usage in other class like this

    constructor TStudentClass.Create()
    begin
    m_objStudentList := TObjectDictionary.Create([doOwnsValues], 256); // created TObjectDictionary object here.
    end;

    procedure TStudentClass.UpdateStudent(nId : Integer; sName : string);
    var objStudent : TStudent;
    begin
    objStudent := m_objStudentList.TryGetValue(nId, objStudent);
    if(objStudent = Nil) then Exit;

    objStudent.StudentName := sName;

    m_objStudentList.AddOrSetValue(nId, objStudent); // setting here. AFTER THIS, objStudent contains some weird values.

    // tried folloing as well but has same results
    //m_objStudentList.Items[nId] := objStudent;
    end;

    So what could be wrong here? Thanks for any suggestions.

    This world is going to explode due to international politics, SOON.

    S 1 Reply Last reply
    0
    • A AmbiguousName

      Hello guys.. I am having this TObjectDictionary object. I can successfully read and write values in this dictionary. But problem comes when I try to update one of my values. When I access a value, change and update it, then it stores some odd object whose values are changed automatically. Here is what I am trying.

      /////////////////////////////////////// storing this type based on 'StudentdId'
      type TStudent = class
      public
      StudentName, StudentClass : string;
      StudentId : Integer;
      end;

      ///////////////////////////////////////// usage in other class like this

      constructor TStudentClass.Create()
      begin
      m_objStudentList := TObjectDictionary.Create([doOwnsValues], 256); // created TObjectDictionary object here.
      end;

      procedure TStudentClass.UpdateStudent(nId : Integer; sName : string);
      var objStudent : TStudent;
      begin
      objStudent := m_objStudentList.TryGetValue(nId, objStudent);
      if(objStudent = Nil) then Exit;

      objStudent.StudentName := sName;

      m_objStudentList.AddOrSetValue(nId, objStudent); // setting here. AFTER THIS, objStudent contains some weird values.

      // tried folloing as well but has same results
      //m_objStudentList.Items[nId] := objStudent;
      end;

      So what could be wrong here? Thanks for any suggestions.

      This world is going to explode due to international politics, SOON.

      S Offline
      S Offline
      smags13
      wrote on last edited by
      #2

      Shouldn't the function signature of TryGetValue look like the following?

      function TryGetValue(const Key: TKey; out Value: TValue): Boolean;

      and the objStudent was corrupted actually after the call to TryGetValue

      if (m_objStudentList.TryGetValue(nId, ObjStudent))
      begin
      objStudent.StudentName:= sName;

      //don't need to call AddOrSetValue as Delphi returns object reference for you.
      end;

      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