Mega Search
23.2 Million


Sign Up

Make a donation  
Why A class variable affects the result of the method?  
News Group: embarcadero.public.delphi.oodesign

This is a simplification of my code, but with this unit, still doen't work. I am with Delphi XE5, and I have the next unit. If a comment the class var ID_COUNTER, the result of the variable 's' showed in the ShowMessage is different that when exists this class variable. According to the documentation a not used class variable never affects the code, but if you check my code in your pc, you will check that it does.

Here is the unit.

{code}
unit Umain;
interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,XMLIntf,XmlDoc,IOUtils,XMLDom,System.Generics.Collections;

type
  TStore = class
    public
    class var ID_COUNTER: Integer;// When comment the result is OK
    MainNode: IDomNode;
    constructor create(node:IDomNode);
     function getNode():IDomNode;
  end;
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
  FMain: TStore;
    function Recursive(node:IDomNode):TStore;

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
 Doc:IXMLDocument;
 content: WideString;
 html: IDomNode;
 s: String;
begin
   Doc := TXMLDocument.Create(Application);
   Doc.LoadFromFile('C:\temp\example.xml');
   Doc.Active := true;
   html := Doc.DOMDocument.getElementsByTagName('html').item[0];
   FMain := Recursive(html);
   s := FMain.getNode().nodeName;
   ShowMessage(s);// is OK if class variable is commented
end;

function TForm1.Recursive(node: IDOMNode):TStore;
var
 i: Integer;
 store: TStore;
 nodeName,nodeValue:String;
begin
  store := TStore.create(node);

  if(not node.hasChildNodes)then
    Exit(store);

  for i := 0 to node.childNodes.length-1 do
  begin
     Recursive(node.childNodes.item[i]);
  end;

  Exit(store);
end;
constructor TStore.create(node: IDOMNode);
begin
  self.MainNode := node;
end;
function TStore.getNode:IDomNode;
begin
   Result := self.MainNode;
end;

end.
{code}

And the code in example.xml, I reduce and now it is just this code:

{code}

  
    
  
  
    border-collapse: separate
    
  

{code}

Of course, my real code is different, with more functions, variables, but if I reduce the unit to this small code, it still have the problem

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 8-Jul-2014, at 4:15 AM EST
From: David RG
 
Re: Why A class variable affects the result of the method?  
News Group: embarcadero.public.delphi.oodesign
That's because you haven't read the documentation and have come to an invalid conclusion
Basically, you've assumed 'class' only applies to that one line, and not the lines following it

You probably wanted
{code}
  type
  TStore = class
    public
    class var ID_COUNTER: Integer;
    public
    MainNode: IDomNode;
    constructor create(node:IDomNode);
    function getNode():IDomNode;
  end;
{code}

See 'Class Fields' at http://docwiki.embarcadero.com/RADStudio/XE/en/Fields
Here's the main part for your convenience

You can introduce a block of class fields within a class declaration by using the class var block declaration. All fields declared after class var have static storage attributes. A class var block is terminated by the following:
 Another class var or var declaration
 A procedure or function (i.e. method) declaration (including class procedures and class functions)
 A property declaration (including class properties)
 A constructor or destructor declaration
 A visibility scope specifier (public, private, protected, published, strict private, and strict protected)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Jul-2014, at 8:32 AM EST
From: karl pritchett