Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Why A class variable affects the result of the method?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Jul 8, 2014 4:44 PM Last Post By: Rudy Velthuis (...
David RG

Posts: 1
Registered: 6/6/13
Why A class variable affects the result of the method?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 8, 2014 1:15 AM
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.

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.


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

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
  </head>
  <body>
    <span style="font-family: verdana, sans-serif;">border-collapse: separate</span>
    
  </body>
</html>


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
karl pritchett

Posts: 15
Registered: 1/6/09
Re: Why A class variable affects the result of the method?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 8, 2014 5:32 AM   in response to: David RG in response to: David RG
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
  type
  TStore = class
    public
    class var ID_COUNTER: Integer;
    public
    MainNode: IDomNode;
    constructor create(node:IDomNode);
    function getNode():IDomNode;
  end;


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)
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Why A class variable affects the result of the method?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 8, 2014 4:44 PM   in response to: David RG in response to: David RG
David RG wrote:

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.

See the answer to exactly the same question on StackOverflow.

--
Rudy Velthuis (TeamB) http://www.teamb.com

"Whatever the natural cause, sin is the true cause of all
earthquakes."
-- John Wesley
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02