Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2020-09

how to access variables between various procedures

28 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mgr. Janusz Chmiel

I have found myself in The complex problem.
How to access The variable which is being declared and used in procedure TQuestion.tiInput( _Sender: TObject; _Event: TJSEvent); in The following other procedure:
procedure TMyApplication.doRun;
I have declared integer variable in The
procedure TQuestion.tiInput( _Sender: TObject; _Event: TJSEvent); and I want to access this variable content in The
procedure TQuestion.tiInput( _Sender: TObject; _Event: TJSEvent); It is impossible for Me and I Am very sad because of it. Is it possible to share variable content between various procedures?

Thank you very very much.

Reply by email

Michael Van Canneyt
to Mgr. Janusz Chmiel
Mgr. Janusz Chmiel wrote 11 lines
I have found myself in The complex problem. How to access The variable which is being declared and used in procedure TQuestion.tiInput( _Sender: TObject; _Event: TJSEvent); in The following other procedure: procedure TMyApplication.doRun; I have declared integer variable in The procedure TQuestion.tiInput( _Sender: TObject; _Event: TJSEvent); and I want to access this variable content in The procedure TQuestion.tiInput( _Sender: TObject; _Event: TJSEvent); It is impossible for Me and I Am very sad because of it. Is it possible to share variable content between various procedures?

You must declare these variables outside the methods, as fields of the TQuestion class.

Michael.

Reply by email

Mgr. Janusz Chmiel
to Michael Van Canneyt

Here is The code. I have tried to replace windowalert command by display info as text since GOogle chrome rendering engine has some internal issues and when I press OK on window alert some editable boxes refuse to receieve characters. I will try to add table on future which will contain correctly answered pairs of The words. But now, I must solve variable issue. Thank you for yours patience to my pleas. I can learn very much about object Pascal thanks to yours advices.

program project1;
{$mode objfpc}
uses
browserapp, JS, Classes, SysUtils, Web, webwidget, htmlwidgets;
type
TMyApplication
=class(TBrowserApplication)
procedure doRun; override;
 end;
{ TQuestion }
TQuestion
=
class
 l: TLabelWidget;
 ti:TTextInputWidget;
                a: integer;
Question, Answer: String;
constructor Create( _ba: TBrowserApplication; _wp: TWebPage; _Question,
_Answer: String);
 procedure tiInput( _Sender : TObject; _Event : TJSEvent);
end;
{ TQuestion }
constructor TQuestion.Create( _ba: TBrowserApplication; _wp: TWebPage;
_Question, _Answer: String); begin
 Question:= _Question;
Answer:=_Answer;
ti:=TTextInputWidget.Create(_ba);
  l:=TLabelwidget.Create(_ba);
l.Text:=_Question;
l.LabelFor:= ti;
l .Parent:= _wp;
ti.Parent:= _wp;
ti.OnInput := @tiInput;
//ti.OnChange:= @tiInput;
 l .Refresh;
ti.Refresh;
end;
procedure TQuestion.tiInput( _Sender: TObject; _Event: TJSEvent);
           begin
 if ti.value = Answer
then
a:=a+1;
           end;
procedure TMyApplication.doRun;
var
 wp: TWebPage;
 tex: TTextWidget;
                                             tex2: TTextWidget;
           begin
 wp:=TWebPage.Create(Self);
wp.Parent:=TViewPort.Create(Nil);
tex:=TTextWidget.Create(Self);
tex.Parent:=wp;
tex.Text:='Doplňte druhého do známé dvojice. Jakmile doplníte správné jméno,
aplikace Vás pochválí. V opačném případě aplikace nevypíše nic.';
tex.Refresh;
                                             tex2:=TTextWidget.Create(Self);
tex2.Parent:=wp;
tex2.Text:='You have answered'+a +'Good answer/s from 36 possible answers';
tex2.Refresh;
TQuestion.Create(Self, wp,'Hamlet a', 'Ofélie');
TQuestion.Create(Self, wp,'Břetislav a', 'Jitka');
 TQuestion.Create( Self, wp,'Radůz a','Mahulena');
 TQuestion.Create( Self, wp,'Laurin a','Klement');
 TQuestion.Create( Self, wp,'Kain a','Ábel');
 TQuestion.Create( Self, wp,'Romeo a','Julie');
 TQuestion.Create( Self, wp,'Othello a','Desdemona');
 TQuestion.Create( Self, wp,'Přemysl a','Libuše');
 TQuestion.Create( Self, wp,'Kleopatra a','Antonius');
 TQuestion.Create( Self, wp,'Faust a','Markéta');
 TQuestion.Create( Self, wp,'Tristan a','Izolda');
 TQuestion.Create( Self, wp,'Adam a','Eva');
 TQuestion.Create( Self, wp,'Cyrano a','Roxana');
 TQuestion.Create( Self, wp,'David a','Goliáš');
 TQuestion.Create( Self, wp,'Oldřich a','Božena');
 TQuestion.Create( Self, wp,'Ruslan a','Ludmila');
 TQuestion.Create( Self, wp,'Kolben a','Daněk');
 TQuestion.Create( Self, wp,'Čert a','Káča');
 TQuestion.Create( Self, wp,'Romulus a','Remus');
 TQuestion.Create( Self, wp,'Hanzelka a','Zikmund');
 TQuestion.Create( Self, wp,'Suchý a','Šlitr');
 TQuestion.Create( Self, wp,'Šimek a','Grossman');
 TQuestion.Create( Self, wp,'Laurel a','Hardy');
 TQuestion.Create( Self, wp,'Voskovec a','Werich');
 TQuestion.Create( Self, wp,'Křemílek a','Vochomůrka');
 TQuestion.Create( Self, wp,'Maková panenka a','motýl Emanuel');
 TQuestion.Create( Self, wp,'Bob a','Bobek');
 TQuestion.Create( Self, wp,'Bolek a','Lolek');
 TQuestion.Create( Self, wp,'Čuk a','Gek');
 TQuestion.Create( Self, wp,'Jeníček a','Mařenka');
 TQuestion.Create( Self, wp,'Spejbl a','Hurvínek');
 TQuestion.Create( Self, wp,'Sultán a','Tyrl');
 TQuestion.Create( Self, wp,'Orfeus a','Eurydika');
 TQuestion.Create( Self, wp,'Ctirad a','Šárka');
 TQuestion.Create( Self, wp,'Samson a','Dalila');
 TQuestion.Create( Self, wp,'Cyril a','Metoděj');
wp.Refresh;
end;
var
 Application : TMyApplication;
begin
 Application:=TMyApplication.Create(nil);
Application.Initialize;
 Application.Run;
end.

Reply by email

Mgr. Janusz Chmiel
to Mgr. Janusz Chmiel

aMay be that it is complex and hydden Pas2js compiler issue related to variable. I have tried to access a variable not only by using some methods from Htmlwidgets unit. Really, variable can not be Am always getting variable not found error. Sure I could simply use The original code from MR Suziau but I have wanted to learn new think thanks to it.
This code can be a very good learning curve. The most complex tasks to implement would be an implementation of methods which are being used when measuging social services according to standards of social services. Various timers which will be always counting how many Milliseconds client spent time to fill every editable field. Do not be afraid, I will fight with so complex thinsks myself. But I must understand why some variables can not be accessed from other procedures.
When this code will work I will create other similar task thanks to it. I have setup Lazarus so I can even run The code directly by using my loving Seamonkey WEB browser.

Reply by email

Mgr. Janusz Chmiel
to Mgr. Janusz Chmiel
I have even tried to type The following line.
tex2.Text:='You have answered'+TQuestion.a +'Good answer/s from 36 possible
answers';
But I have got The following error.
project1.lpr(65,42) Error: Instance member "a" inaccessible here
So really, some magic block variable access from other procedures.

Reply by email

Michael Van Canneyt
to Mgr. Janusz Chmiel
Mgr. Janusz Chmiel wrote 6 lines
I have even tried to type The following line. tex2.Text:='You have answered'+TQuestion.a +'Good answer/s from 36 possible answers'; But I have got The following error. project1.lpr(65,42) Error: Instance member "a" inaccessible here So really, some magic block variable access from other procedures.

No, you do not seem to understand how classes work.

The variable a is part of a TQuestion class. You create several instances of the TQuestion class. TQuestion.A does not exist because a is part of an instance.

So, You need to save the instances you create in an array.

var
   MyQuestion : Array[1..36] of TQuestion;
For I:=1 to 36 do
   MyQuestion[i]:=TQuestion.Create(blah);

Then you can do for example

if MyQuestion[13].A=1 then

and so on.

Michael.

Reply by email

Michael Van Canneyt
to Mgr. Janusz Chmiel
Mgr. Janusz Chmiel wrote
Oh. So no simply use an integer variable what I have tried to accomplish. Thank you very much for yours answer.

You can always introduce a global variable a in your unit, but this will most likely give problems later on.
I would not recommend it.

Michael.

Reply by email

Mgr. Janusz Chmiel
to Michael Van Canneyt

Can be global variable declared by using public directive? But When i have tried to use public yesterday I had no chance and I have got also error. I have only tried to simply have a variable which would work as a counter. So when user answer right question, The right answer whould be counted by using a=a+1.
But I know, that I must respect The characteristics of object Pascal programming, because I can not mix non object based programming with object one.

Reply by email

Michael Van Canneyt
to Mgr. Janusz Chmiel
Mgr. Janusz Chmiel wrote
Can be global variable declared by using public directive?

Just add

   var a : Integer;

after the implementation keyword in your unit.

Show quoted text 8 lines
But When i have tried to use public yesterday I had no chance and I have got also error. I have only tried to simply have a variable which would work as a counter. So when user answer right question, The right answer whould be counted by using a=a+1. But I know, that I must respect The characteristics of object Pascal programming, because I can not mix non object based programming with object one.

You can, you just need to use the right location for your variable.

Michael.

Reply by email

Mgr. Janusz Chmiel
to Michael Van Canneyt

Find where to add implementation keyword is really adventure. But thank you for yours time and for yours help. I have tried to add implementation
var a: integer;
to The various places inside The source code. But I AM getting syntax error or
identifier expected errors.

Reply by email

Michael Van Canneyt
to Mgr. Janusz Chmiel
Mgr. Janusz Chmiel wrote 7 lines
Find where to add implementation keyword is really adventure. But thank you for yours time and for yours help. I have tried to add implementation var a: integer; to The various places inside The source code. But I AM getting syntax error or identifier expected errors.

If you are just using a program, add it after (or before) the type section.

Var
   A : Integer;
Type
   TQuestion = Class
   end;

Michael.

Reply by email

Mgr. Janusz Chmiel
to Michael Van Canneyt

aaI had to give it up The fight with integer variable. So I will simply use windowalert which will inform The user about number of right answers from
36. I had A dream to create text block with this info, but when I see how is not easy to manipulate with variables across The procedures. I will have to experience Pascal summer time sadness thanks to variables access across The procedures.

Reply by email

Michael Van Canneyt
to Mgr. Janusz Chmiel
Mgr. Janusz Chmiel wrote 6 lines
aaI had to give it up The fight with integer variable. So I will simply use windowalert which will inform The user about number of right answers from 36. I had A dream to create text block with this info, but when I see how is not easy to manipulate with variables across The procedures. I will have to experience Pascal summer time sadness thanks to variables access across The procedures.

If you can post your program so it does 99% of what you need, then someone her can maybe modify it so it does what you want.

From what you describe, it should be trivial to accomplish,

but from mails without actual code I cannot see where your problem is.

Michael.

Reply by email

Mgr. Janusz Chmiel
to Michael Van Canneyt

Yes, with pleasure. I Am sending you as A attachment, since copy and paste and Lazarus makes some TAB which are transferred to text of E-mail messages as a several Spaces and it is not easy to remove those spaces and compile. I want to have a variable functioning. When this code will work I will make second totally myself and I will not intrude you again. I AM very sorry that I Am so big Pascal chaos maker. But I Am hoping, that I will learn better and better Pascal thanks to you. YOu can not imagine how is positive ffeling for Me having fast and reliable WEB applications available for so many various devices, operating systems. No fight with Android APIS, which create table and without sighted user good number setup it display very ugly table. Thanks to yours unit, everything is really possible.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: project1.lpr
Type: application/octet-stream
Size: 3569 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20200915/0d0ee47f/attachment-0001.obj>

Reply by email

Mgr. Janusz Chmiel
to warleyalex

aaaThank you for yours excellent advice. But I Am getting new error now. The code
tex2.Text:='You have answered'+a +'Good answer/s from 36 possible answers'; generate The other complex error
project1.lpr(67,31) Error: Operator is not overloaded: "String" + "Longint"

Reply by email

Mgr. Janusz Chmiel
to warleyalex

Oh, thank you very much for yours useful advice and help. I AM very nicely impressed how kind developers are in this mailing list here. There are many kind people around The world. And this mailing list is The best evidence about this fact.

Reply by email

Mgr. Janusz Chmiel
to Mgr. Janusz Chmiel

I have helped myself by using consolebrowser unit and writeln command I have set MaxConsoleLines to 1 line. And in this case, I can have what I have wanted. If I answer good answer, app write The number of good answers. Sure, it look not very kind for sighted professionals, but I Am very glad, that it is safely possible to combine console output with HTMLwidgets unit output without crashes. Really. The units of Pas2js are full of positive surprices. Thank all of you for yours patience with Me. FOrtunately, it is possible to code by using various techniques. And It is excellent.

Reply by email

Michael Van Canneyt
to Mgr. Janusz Chmiel
Mgr. Janusz Chmiel wrote 8 lines
I have helped myself by using consolebrowser unit and writeln command I have set MaxConsoleLines to 1 line. And in this case, I can have what I have wanted. If I answer good answer, app write The number of good answers. Sure, it look not very kind for sighted professionals, but I Am very glad, that it is safely possible to combine console output with HTMLwidgets unit output without crashes. Really. The units of Pas2js are full of positive surprices. Thank all of you for yours patience with Me. FOrtunately, it is possible to code by using various techniques. And It is excellent.

Attached is your project as you presumably intended it.

I moved the "a" variable to a field in the Tapplication class, and made a procedure IncScore which is called when a correct answer is provided.
It updates "a" and refreshes the score text.

For this I moved the score text label from a local variable to a field in the Tapplication class.

Tested in the browser, it works just fine.

And I made the code a little more readable for people that must see code in order to understand it... I am amazed you manage to program at all, my deepest respect.

Hopefully this example helps you along.

Michael.
-------------- next part --------------
program project1;
{$mode objfpc}
uses
browserapp, JS, Classes, SysUtils, Web, webwidget, htmlwidgets;
type
  TMyApplication =class(TBrowserApplication)
    a : Integer;
    tex2: TTextWidget;
    Procedure IncScore;
    procedure doRun; override;
  end;
  { TQuestion }
  TQuestion = class
     l: TLabelWidget;
     ti:TTextInputWidget;
    Question, Answer: String;
    constructor Create( _ba: TBrowserApplication; _wp: TWebPage; _Question, _Answer: String);
    procedure tiInput( _Sender : TObject; _Event : TJSEvent);
  end;
var
  Application : TMyApplication;

{ TQuestion }

constructor TQuestion.Create( _ba: TBrowserApplication; _wp: TWebPage; _Question, _Answer: String);
begin
  Question:= _Question;
  Answer:=_Answer;
  ti:=TTextInputWidget.Create(_ba);
  l:=TLabelwidget.Create(_ba);
  l.Text:=_Question;
  l.LabelFor:= ti;
  l .Parent:= _wp;
  ti.Parent:= _wp;
  //ti.OnInput := @tiInput;
  ti.OnChange:= @tiInput;
  l .Refresh;
  ti.Refresh;
end;
procedure TQuestion.tiInput( _Sender: TObject; _Event: TJSEvent);
begin
  if ti.value = Answer then
    Application.IncScore;
end;
 Procedure TMyApplication.IncScore;
begin
  a:=a+1;
  tex2.Text:='You have answered'+IntToStr(a) +'Good answer/s from 36 possible answers';
end;
procedure TMyApplication.doRun;
var
  wp: TWebPage;
  tex: TTextWidget;
begin
  wp:=TWebPage.Create(Self);
  wp.Parent:=TViewPort.Create(Nil);
  tex:=TTextWidget.Create(Self);
  tex.Parent:=wp;
  tex.Text:='Doplňte druhého do známé dvojice. Jakmile doplníte správné jméno, aplikace Vás pochválí. V opačném případě aplikace nevypíše nic.';
  tex.Refresh;
  tex2:=TTextWidget.Create(Self);
  tex2.Parent:=wp;
  tex2.Text:='You have answered'+IntToStr(a) +'Good answer/s from 36 possible answers';
  tex2.Refresh;
 TQuestion.Create(Self, wp,'Hamlet a', 'Ofélie');
 TQuestion.Create(Self, wp,'Břetislav a', 'Jitka');
 TQuestion.Create( Self, wp,'Radůz a','Mahulena');
 TQuestion.Create( Self, wp,'Laurin a','Klement');
 TQuestion.Create( Self, wp,'Kain a','Ábel');
 TQuestion.Create( Self, wp,'Romeo a','Julie');
 TQuestion.Create( Self, wp,'Othello a','Desdemona');
 TQuestion.Create( Self, wp,'Přemysl a','Libuše');
 TQuestion.Create( Self, wp,'Kleopatra a','Antonius');
 TQuestion.Create( Self, wp,'Faust a','Markéta');
 TQuestion.Create( Self, wp,'Tristan a','Izolda');
 TQuestion.Create( Self, wp,'Adam a','Eva');
 TQuestion.Create( Self, wp,'Cyrano a','Roxana');
 TQuestion.Create( Self, wp,'David a','Goliáš');
 TQuestion.Create( Self, wp,'Oldřich a','Božena');
 TQuestion.Create( Self, wp,'Ruslan a','Ludmila');
 TQuestion.Create( Self, wp,'Kolben a','Daněk');
 TQuestion.Create( Self, wp,'Čert a','Káča');
 TQuestion.Create( Self, wp,'Romulus a','Remus');
 TQuestion.Create( Self, wp,'Hanzelka a','Zikmund');
 TQuestion.Create( Self, wp,'Suchý a','Šlitr');
 TQuestion.Create( Self, wp,'Šimek a','Grossman');
 TQuestion.Create( Self, wp,'Laurel a','Hardy');
 TQuestion.Create( Self, wp,'Voskovec a','Werich');
 TQuestion.Create( Self, wp,'Křemílek a','Vochomůrka');
 TQuestion.Create( Self, wp,'Maková panenka a','motýl Emanuel');
 TQuestion.Create( Self, wp,'Bob a','Bobek');
 TQuestion.Create( Self, wp,'Bolek a','Lolek');
 TQuestion.Create( Self, wp,'Čuk a','Gek');
 TQuestion.Create( Self, wp,'Jeníček a','Mařenka');
 TQuestion.Create( Self, wp,'Spejbl a','Hurvínek');
 TQuestion.Create( Self, wp,'Sultán a','Tyrl');
 TQuestion.Create( Self, wp,'Orfeus a','Eurydika');
 TQuestion.Create( Self, wp,'Ctirad a','Šárka');
 TQuestion.Create( Self, wp,'Samson a','Dalila');
 TQuestion.Create( Self, wp,'Cyril a','Metoděj');
  wp.Refresh;
  terminate;
end;
begin
  Application:=TMyApplication.Create(nil);
  Application.Initialize;
  Application.Run;
end.

Reply by email

Mgr. Janusz Chmiel
to Michael Van Canneyt

Dear MR Van Canneyt,
Sure. Yours approach is The best and 100 % what I have desired to reach. You are elite developer. You are author of Pas2js many of its unit and you are really truly professional who is working on complex projects. I will have to learn for many years from you before I will reach yours programmers level. And I AM afraid, that I will be never so excellent developer like you are. Thank you that you are so kind and that you have wasted yours time to help Me. Really, very well done!!!

Reply by email