View Issue Details

IDProjectCategoryView StatusLast Update
0002992JEDI VCL00 JVCL Componentspublic2005-06-14 05:25
ReporterWBennerAssigned Toobones 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version3.00 
Target VersionFixed in Version3.10 
Summary0002992: Assign in tjvSimpleXMLElem works not correct
DescriptionThe Assign-Statement of tjvSimpleXMLElem-Class makes no difference between the classes tjvSimpleXMLElem, tjvSimpleXMLComment and tjvSimpleXMLCData, alsways tjvSimpleXMLElem is assigned.
Additional InformationHere is my solution for this problem:

procedure TJvSimpleXMLElem.Assign(Value: TJvSimpleXMLElem);
var
  Elems: TJvSimpleXMLElem;
  Elem: TJvSimpleXMLElem;
  I: Integer;
begin
  Clear;
  if Value = nil then
    Exit;
  Elems := TJvSimpleXMLElem(Value);
  Name := Elems.Name;
  Self.Value := Elems.Value;
  for I := 0 to Elems.Properties.Count - 1 do
    Properties.Add(Elems.Properties[I].Name, Elems.Properties[I].Value);

  for I := 0 to Elems.Items.Count - 1 do
  begin
    If Elems.Items[i] is tjvSimpleXMLElemComment then
      Elem := Items.AddComment(Elems.Items[I].Name, Elems.Items[I].Value)
    else
    If Elems.Items[i] is tjvSimpleXMLElemCData then
      Elem := Items.AddCData(Elems.Items[I].Name, Elems.Items[I].Value)
    else
    If Elems.Items[i] is tjvSimpleXMLElem then
      Elem := Items.Add(Elems.Items[I].Name, Elems.Items[I].Value);

    Elem.Assign(TJvSimpleXMLElem(Elems.Items[I]));
  end;
end;
TagsNo tags attached.

Activities

obones

2005-05-25 11:10

administrator   ~0007344

This definitely is NOT a good solution.
Assign should be overriden in the derived classes.

obones

2005-05-25 11:27

administrator   ~0007347

Further to this, what's the whole point of your mod ?
I don't quite get it, is there a bug ?

WBenner

2005-05-27 01:05

reporter   ~0007362

This IS a good solution because it works and because it is necessary. The original code had a bug. If a tjvsimpleXMLElem object contains some members of different classes how tjvsimpleXMLComment and tjvsimpleXMLCData and you want assign this to a new object then ALL members will assigned as tjvsimpleXMLElem!

obones

2005-06-01 06:41

administrator   ~0007378

Ok, I agree on the "bug", but NO your solution is NOT a good solution as it does not allow for easy extension. As a principle, a base class should NEVER know a single thing about its derived classes.
There is however a solution to this problem, where you replace the existing loop with this one:

  for I := 0 to Elems.Items.Count - 1 do
  begin
    Elem := TJvSimpleXMLElemClass(Elems.Items[I].ClassType).Create(Elems.Items[I].Parent);
    Elem.Assign(Elems.Items[I]);
    Items.Add(Elem);
  end;

With TJvSimpleXMLElemClass declared as "class of TJvSimpleXMLElem".
This way, the virtual constructor of the derived class is always called, and the parent does not have to know about it.
I've tried here, it works, but please tell us if it works as well on your end. Only then this will go in CVS.

obones

2005-06-14 05:25

administrator   ~0007458

Well, no answers in the past 14 days, the proposed code is in CVS.

Issue History

Date Modified Username Field Change
2005-05-25 01:09 WBenner New Issue
2005-05-25 11:10 obones Note Added: 0007344
2005-05-25 11:27 obones Note Added: 0007347
2005-05-27 01:05 WBenner Note Added: 0007362
2005-06-01 06:41 obones Note Added: 0007378
2005-06-01 06:41 obones Status new => feedback
2005-06-14 05:25 obones Status feedback => resolved
2005-06-14 05:25 obones Resolution open => fixed
2005-06-14 05:25 obones Assigned To => obones
2005-06-14 05:25 obones Note Added: 0007458