이 예제는 물론 단순화에서 가져온 객체하지만 기본적으로 나는 내 자신의 개체 메인에 생성됩니다델파이 : 저장소에 올바른 방법은 TObjectList와
function Execute(var aSettings: TSettings):Boolean
TSettings
와 다른 형태 (frmSettings)을 트리거하는 기본 폼이 양식을 추적하기위한 양식.
새로 열린이 양식 (frmSettings)에서 나는 TObjectList
의 자손 인 TMyObjectList
을 가져옵니다. TMyObj
으로 채워져 있습니다.
그런 다음 해당 TMyObjectList의 값으로 TListBox
을 채 웁니다.
:
...
FMyObjectList : TMyObjectList;
property MyObjectList: TMyObjectList read getMyObjectList;
...
function TfrmSettings.getMyObjectList: TMyObjectList ;
begin
If not Assigned(FMyObjectList) then FMyObjectList := TMyObjectList.Create(True)
Result := FMyObjectList;
end;
function TfrmSettings.Execute(var aSettings: TSettings): Boolean;
begin
//Fill myObjectList
FetchObjs(myObjectList);
//Show list to user
FillList(ListBox1, myObjectList);
//Show form
ShowModal;
Result := self.ModalResult = mrOk;
if Result then
begin
// Save the selected object, but how??
// Store only pointer? Lost if list is destroyed.. no good
//Settings.selectedObj := myObjectList.Items[ListBox1.ItemIndex];
// Or store a new object? Have to check if exist already?
If not Assigned(Settings.selectedObj) then Settings.selectedObj := TMyObj.Create;
Settings.selectedObj.Assign(myObjectList.Items[ListBox1.ItemIndex];);
end;
end;
procedure TfrmSettings.FillList(listBox: TListBox; myObjectList: TMyObjectList);
var
i: Integer;
begin
listBox.Clear;
With myObjectList do
begin
for i := 0 to Count - 1 do
begin
//list names to user
listBox.Items.Add(Items[i].Name);
end;
end;
end;
procedure TfrmSettings.FormDestroy(Sender: TObject);
begin
FreeAndNil(FMyObjectList);
end;
설정을 트리거로, 단지 포인터가 좋은 생각으로 보이지 않는다 저장 다시 형성, "목록을 재현하고 원래 목적은 심지어 사용자의 조회수가 삭제 될 수 취소 "
그래서 복사본을 저장하면 할당을 사용하여 모든 속성이 올바로 표시됩니다. 먼저 객체가 있는지 먼저 확인하십시오.
If not Assigned(Settings.selectedObj) then Settings.selectedObj := TMyObj.Create;
Settings.selectedObj.Assign(myObjectList.Items[ListBox1.ItemIndex];);
나는이 올바른 보입니까 대신 Settings.AssignSelectedObj(aMyObj:TMyObj)
같은 방법으로 두 줄을 이동해야하거나 나는이 길을 잘못 구현 무엇입니까? 더/덜 필요한 것이 있습니까?
나는 메모리 누수 및 기타 문제가 발생하지 않도록보다 안전하다고 느끼기 위해 몇 가지 지침이 필요합니다.
코드를 검토하는 것 외에 다른 질문이 있습니다. 이 설정 방법은 SelectedObject를 설정 클래스에 저장하는 올바른 방법입니까?
할당되지 않은 경우 (FMyObjectList) FMyObjectList.Create (True); 올바르지 않습니다 - 작성해야합니다 : 할당되지 않은 경우 (FMyObjectList) 다음 FMyObjectList : = TMyObjectList.Create (True); –
@ A.Bouchez : 참. 명시된 바와 같이, 그것은 예제 코드이기 때문에 때로는 삭제 버튼을 너무 세게 치기도합니다. 그래도 그것을 지적 해 주셔서 고맙다, 편집! – Bulan