InLine variable declaration, WORKS, if used correctly
my example in RAD Studio 10.3.1 Arch
[SHOWTOGROUPS=4,19,20]
[/SHOWTOGROUPS]
[SHOWTOGROUPS=4,19,20]
Код:
unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
procedure prcPrivateProc(lVarA: Integer);
public
procedure prcPublicProc(lVarB: Integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
//
{
//****************************************************************
procedure prcSub_Procedure1;
begin
s := s + ', i dont work in prcSub_Procedure1! See my definition is below (later)';
end;
//****************************************************************
}
//
var // <---
// i: Integer; // if defined here, will conflict with "i" defined in "var inline below"
//
s: String; // scope global to your procedure, same if used in sub-procedures defined here!
lUrl: String;
//
//
// ****************************************************************
procedure prcSub_Procedure2;
begin
s := s + ', im re-writed in prcSub_Procedure2! See my definition is above (before)';
end;
// ****************************************************************
begin
//
// DONT WORRY ABOUT "line red" in your editor screen!
//
lUrl := 'http://www.idera.com';
//
ComboBox1.Items.Clear;
begin // ITS NOT NECESSARY, AND DONT HELP YOU AND NOTHING HERE!
//
var ext := LowerCase(ExtractFileExt(lUrl));
ShowMessage(ext); // works!
prcPrivateProc(ext.ToInteger()); // works!
prcPublicProc(ext.ToInteger()); // works!
for var i := 0 to 10 do { var "inline" only works on the "block" is defined! }
begin // "BEGIN_NECESSARY" --> if commented, i is "out of scope" too
//
ComboBox1.Items.Add(Format('Value to i = %d', [i]));
//
ComboBox1.ItemIndex := i; // "BEGIN_NECESSARY" (above) is essential to works! and below definitions
//
//
prcPrivateProc(i); // works! "i" on the scope!
//
prcPublicProc(i); // works! "i" on the scope!
//
{
if (i = 4) then // works too, with BEGIN
break;
}
//
if (i = 10) then
ShowMessage(Format('Value to i = %d', [i])); // works! "i" on the scope!
//
end; // end to "BEGIN_NECESSARY"
//
// prcPrivateProc(i); // dont works! "i" out of scope!
//
// prcPublicProc(i); // ont works! "i" out of scope!
//
// ShowMessage(Format('Value to i = %d', [i])); // dont works! "i" out of scope!
//
end;
//
// prcPrivateProc(i); // dont works! "i" out of scope!
//
// prcPublicProc(i); // dont works! "i" out of scope!
//
// ShowMessage(Format('Value to i = %d', [i])); // dont works! "i" out of scope!
//
end;
procedure TForm1.Button2Click(Sender: TObject);
//
{
//****************************************************************
procedure prcSub_Procedure1;
begin
s := s + ', i dont work in prcSub_Procedure1! See my definition is below (later)';
end;
//****************************************************************
}
//
var // <---
// i: Integer; // if defined here, will conflict with "i" defined in "var inline below"
//
s: String; // scope global to your procedure, same if used in sub-procedures defined here!
lUrl: String;
//
//
// ****************************************************************
procedure prcSub_Procedure2;
begin
s := s + ', im re-writed in prcSub_Procedure2! See my definition is above (before)';
end;
// ****************************************************************
begin
//
// DONT WORRY ABOUT "line red" in your editor screen! HERE NONE RED-LINE APPEARS!
//
lUrl := 'http://www.idera.com';
//
ComboBox1.Items.Clear;
//
// begin // BEGIN_1 { uncomment here and showmessage and my procedure prcPrivate and prcPublic}
//
var i := Random(20); { var "inline" only works on the "block" is defined! }
var ext := LowerCase(ExtractFileExt(lUrl));
//
begin // BEGIN_2 // its not necessary to ShowMessage or my procedure prcPrivate and prcPublic works
//
ComboBox1.Items.Add(Format('Value to i = %d', [i]));
//
ComboBox1.ItemIndex := i; // "BEGIN_NECESSARY" (above) is essential to works! and below definitions
//
//
prcPrivateProc(i); // works! "i" on the scope!
//
prcPublicProc(i); // works! "i" on the scope!
//
if (i = 10) then
ShowMessage(Format('Value to i = %d', [i])); // works! "i" on the scope!
//
end; // end to BEGIN_2
//
prcPrivateProc(i); // works to BEGIN_1 and BEGIN_2 defined
//
prcPublicProc(i); // works to BEGIN_1 and BEGIN_2 defined
//
ShowMessage(Format('Value to i = %d', [i])); // works to BEGIN_1 and BEGIN_2 defined
//
// end; // end to BEGIN_1
//
prcPrivateProc(i); // dont works! if BEGIN_1 defined, then "i"/"ext" out of scope!
//
prcPublicProc(i); // dont works! if BEGIN_1 defined, then "i"/"ext" out of scope!
//
ShowMessage(Format('Value to i = %d', [i])); // dont works! if BEGIN_1 defined, then "i"/"ext" out of scope!
//
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ShowMessage('destroyng....');
end;
procedure TForm1.prcPrivateProc(lVarA: Integer);
begin
ShowMessage(Format('lVarA = %d', [lVarA]));
end;
procedure TForm1.prcPublicProc(lVarB: Integer);
begin
ShowMessage(Format('lVarB = %d', [lVarB]));
end;
end.
[/SHOWTOGROUPS]
Последнее редактирование: