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