Recent

Author Topic: Array properties [SOLVED]  (Read 4683 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Array properties [SOLVED]
« on: May 17, 2021, 02:39:36 pm »
في شذرة الكود التالي والتي في هذا الرابط https://www.freepascal.org/docs-html/current/ref/refsu35.html#x88-1100006.7.3 - الذي يشرح خصائص الأصناف من نوع المصفوفة - لا أرى أي إعلانٍ لمصفوفة أم أنّ إعلان المصفوفة يكون بداخل الـ methods ولا أعتقد ذلكـ ﻷنّها بذلكـ تكون مصفوفة مؤقّتة أم أنّ هذا أسلوب مصفوفة ديناميكيّة ؟

google translate:

"In the following code snippet "chunk" which is in this link https://www.freepascal.org/docs-html/current/ref/refsu35.html#x88-1100006.7.3 - which explains the properties of the classes of the array type - I don't see any array declaration or is the array declaration inside the methods and I don't think so because it is thus a temporary array or is this a dynamic array style?"

Code: Pascal  [Select][+][-]
  1. Type  
  2.   TIntList = Class  
  3.   Private  
  4.     Function GetInt (I : Longint) : longint;  
  5.     Function GetAsString (A : String) : String;  
  6.     Procedure SetInt (I : Longint; Value : Longint;);  
  7.     Procedure SetAsString (A : String; Value : String);  
  8.   Public  
  9.     Property Items [i : Longint] : Longint Read GetInt  
  10.                                            Write SetInt;  
  11.     Property StrItems [S : String] : String Read GetAsString  
  12.                                             Write SetAsstring;  
  13.   end;  
  14.  
« Last Edit: May 17, 2021, 09:47:19 pm by pascal111 »

ASerge

  • Hero Member
  • *****
  • Posts: 1796
Re: Array properties
« Reply #1 on: May 17, 2021, 02:55:02 pm »
I don't see any array declaration or is the array declaration inside the methods and I don't think so because it is thus a temporary array or is this a dynamic array style?"
From the mentioned documentation "These are properties that accept an index, just as an array does."
Code: Pascal  [Select][+][-]
  1. property Items[i: LongInt]: ...
  2. property StrItems[S: string]: ...

Handoko

  • Hero Member
  • *****
  • Posts: 4292
  • My goal: build my own game engine using Lazarus
Re: Array properties
« Reply #2 on: May 17, 2021, 02:58:42 pm »
Code: Pascal  [Select][+][-]
  1. property Something[i : Sometype] //  is an array property.

Don't be confused with the static array or dynamic array. They all have similarity but different.

lucamar

  • Hero Member
  • *****
  • Posts: 4025
Re: Array properties
« Reply #3 on: May 17, 2021, 04:02:16 pm »
The point is that the programmer accesses the property as if it were an array, no matter what the actual implementation is: it might be a real array, some container class (e.g. TStrings ot TList) or whatever, but from the class user point of view it behaves (mostly) as an array.

Some examples are TComponent.Components[] and TWinControl.Controls[], which are implemented through an internal TFPList.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Array properties
« Reply #4 on: May 17, 2021, 04:40:01 pm »
The point is that the programmer accesses the property as if it were an array, no matter what the actual implementation is: it might be a real array, some container class (e.g. TStrings ot TList) or whatever, but from the class user point of view it behaves (mostly) as an array.

Some examples are TComponent.Components[] and TWinControl.Controls[], which are implemented through an internal TFPList.

من كلامكـ أفهم أنّ الخاصيّة تُربط بنوع بيانات ما قد يكون مصفوفة حقيقيّة أو حاوية ... الخ ،وأنّ هذا الربط يكون داخليّاً كما في حالة TComponent.Components[] و TWinControl.Controls[] ،ولكن أين يكمن هذا الربط الداخلي حيثُ لو كان بداخل الـ methods لكان مؤقتاً فأتوقع أن يكون داخل بنية الصنف نفسها أي عند بناء الصنف ليكون مثلاً شحن الخاصيّة بالبيانات دائماً طيلة فترة حياة الكائن؟

google translate:

"From your words, I understand that the property is linked to some type of data, which may be a real array or container ... etc., and that this connection is internal as in the case of TComponent.Components [] and TWinControl.Controls [] , but where is this internal linking "laying" "for whereas" where if it was inside the methods, it would have been temporary, so I expect it to be inside the class structure itself, i.e. when building the class so that, for example, shipping "assigning" the property with data is permanent throughout the life of the object?"
« Last Edit: May 17, 2021, 04:53:23 pm by pascal111 »

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Array properties
« Reply #5 on: May 17, 2021, 04:46:08 pm »
I don't see any array declaration or is the array declaration inside the methods and I don't think so because it is thus a temporary array or is this a dynamic array style?"
From the mentioned documentation "These are properties that accept an index, just as an array does."
Code: Pascal  [Select][+][-]
  1. property Items[i: LongInt]: ...
  2. property StrItems[S: string]: ...

يبدو أنّ المعنى فعلاً هو أنّ هذه الخصائص إنّما تسلكـ سلوكـ المصفوفات لذلكـ شُبّهت بها.

google translate:

"It seems that the meaning really is that these properties but rather behave like arrays, so they are likened to them."

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Array properties
« Reply #6 on: May 17, 2021, 04:50:02 pm »
Code: Pascal  [Select][+][-]
  1. property Something[i : Sometype] //  is an array property.

Don't be confused with the static array or dynamic array. They all have similarity but different.

كلامكـ صحيح لكن الإشكالية كما ذكر @lucamar في الربط الداخلي مع نوع آخر من البيانات والذي قد يكون مصفوفة فعليّه أو غيرها.

google translate:

"Your statement "speaking" is correct, but the problem "matter", as mentioned by @lucamar, is in the internal linking with another type of data, which may be an actual array or something else."

lucamar

  • Hero Member
  • *****
  • Posts: 4025
Re: Array properties
« Reply #7 on: May 17, 2021, 05:32:31 pm »
[...] "assigning" the property with data is permanent throughout the life of the object?

Yes, of course. Otherwise it would make little sense.

Suppose you want a class to hold some strings, kind of a "light" version of TStringList, which you could access either by their index of by looking up its contents:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyStrings = class
  3.   private
  4.     fStrings: TStringArray; {= "array of Strings"}
  5.     function GetItems(Index: Integer): String;
  6.     procedure SetItems(Index: Integer; AValue: String);
  7.     function GetIndexOf(What: String): Integer;
  8.   public
  9.     property Items[Index: Integer]: String read GetItems write SetItems;
  10.     property IndexOf[What: String]: Integer read GetIndexOf;
  11.   end;

We have implemented it by using a real array, though it doesn't really matters, and since it belongs to the object, whatever we do with it, it will be there as long as the instance itself exits, so we can for example add a string using:
Code: Pascal  [Select][+][-]
  1. MyStrings.Items[8] := 'Something or other';
which will invoke the setter:
Code: Pascal  [Select][+][-]
  1. procedure TMyStrings.SetItems(Index: Integer; AValue: String);
  2. begin
  3.   if Length(fStrings) <= Index then
  4.     SetLength(fStrings, Index+1);
  5.   fStrings[Index] := AValue;
  6. end;
and we can now retrieve it by turning around and doing:
Code: Pascal  [Select][+][-]
  1. StrIndex := MyStrings.IndexOf['Something or other'];
with the security that, if we programmed it properly, the later won't fail and StrIndex will be eight ( 8 ).
« Last Edit: May 17, 2021, 07:13:54 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Array properties
« Reply #8 on: May 17, 2021, 05:50:27 pm »
Code: Pascal  [Select][+][-]
  1. StrIndex := MyStrings.String['Something or other'];

هل تعني:

google translate:

"Do you mean:"

Code: Pascal  [Select][+][-]
  1. StrIndex := MyStrings.IndexOf['Something or other'];
  2.  

lucamar

  • Hero Member
  • *****
  • Posts: 4025
Re: Array properties
« Reply #9 on: May 17, 2021, 07:15:11 pm »
Do you mean:
Code: Pascal  [Select][+][-]
  1. StrIndex := MyStrings.IndexOf['Something or other'];

Oops! Yes, that's what I meant ... sorry, corrected now :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Array properties
« Reply #10 on: May 17, 2021, 07:25:51 pm »
Do you mean:
Code: Pascal  [Select][+][-]
  1. StrIndex := MyStrings.IndexOf['Something or other'];

Oops! Yes, that's what I meant ... sorry, corrected now :-[

لا عليكـ ،إنّما كان خطأً في الكتابة وليس خطأً في منطق البرنامج ،المهم أن يكون منطق البرنامج سليما  :) .

google translate:

"No, you should "There's no burden(blaming) on you" , but it was a mistake in writing and not an error in the logic of the program, the important thing is that the logic of the program is correct :)."

 

TinyPortal © 2005-2018