Recent

Author Topic: [Solved] Error: Only class methods, class properties and  (Read 5661 times)

nikel

  • Full Member
  • ***
  • Posts: 186
[Solved] Error: Only class methods, class properties and
« on: December 16, 2018, 09:32:29 pm »
Hi I'm trying to access variable of a class but getting this error:
Quote
Error: Only class methods, class properties and class variables can be referred with class references

Here's my code:
Code: Pascal  [Select][+][-]
  1. unit Lng;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils,
  9.   dom, xmlread, XMLUtils, XMLPropStorage;
  10.  
  11. type
  12.  
  13.   {LanguageUnit}
  14.  
  15.   TLanguageUnit1=class
  16.  
  17.     procedure Create(Sender: TObject);
  18.  
  19. const
  20.  
  21.   private
  22.     FActionLblCaption: String;
  23. ...

Main Form:
Code: Pascal  [Select][+][-]
  1.   ShowMessage(lng.TLanguageUnit1.ActionLblCaption);

How can I fix this?
« Last Edit: December 16, 2018, 11:19:24 pm by nikel »

jamie

  • Hero Member
  • *****
  • Posts: 6133
Re: Error: Only class methods, class properties and class variable
« Reply #1 on: December 16, 2018, 09:56:01 pm »
its in the private section, not doable..
 
employable a Property so that you can have a getter Function..
 Function GetLangString:String;
Public
 property Lang :String; Read GetLangString;

End;

Implementation

 TLanguageUnit1.GetLangStriing:String;
  Begin
   Result := fActionLB1Caption;
 End;

That is off the top of my head...
you'll need to work that out
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3238
  • POKE 54296,15
    • Eye-Candy Controls
Re: Error: Only class methods, class properties and class variable
« Reply #2 on: December 16, 2018, 09:58:42 pm »
You can only access that property via instance of a class, not the class itself.

You need something like:
Code: Pascal  [Select][+][-]
  1. LanguageUnit1:=TLanguageUnit1.Create;
and then
Code: Pascal  [Select][+][-]
  1. lng.LanguageUnit1.ActionLblCaption
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jamie

  • Hero Member
  • *****
  • Posts: 6133
Re: Error: Only class methods, class properties and class variable
« Reply #3 on: December 16, 2018, 10:50:29 pm »
I must be getting over the hill, I didn't notice he was doing Class access like that..

The only true wisdom is knowing you know nothing

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Error: Only class methods, class properties and class variable
« Reply #4 on: December 16, 2018, 11:15:14 pm »
Hi thanks for the replies. Initializing the class fixed the problem. But now I can't access function:

Code: Pascal  [Select][+][-]
  1. LanguageUnit:=TLanguageUnit1.Create;
  2. LanguageUnit.SetText;

I'm getting weird error.

EDIT: My problem isn't accessing a function. I'll mark this subject as solved.
« Last Edit: December 16, 2018, 11:18:47 pm by nikel »

 

TinyPortal © 2005-2018