Recent

Author Topic: Dynamic arrays in pascal?  (Read 2535 times)

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Dynamic arrays in pascal?
« on: August 05, 2018, 03:03:22 pm »
I have this code:
Code: Pascal  [Select][+][-]
  1.  
  2. var
  3.   where: array of String;
  4. begin
  5.   SetLength(where, 0);
  6.   ShowMessage(String(Length(where)));

...just shows an empty messagebox... :o
While it should show the length of the array where, that is 0.
What do I do wrong?
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Dynamic arrays in pascal?
« Reply #1 on: August 05, 2018, 03:04:22 pm »
Code: Pascal  [Select][+][-]
  1. var
  2.   where: array of String;
  3. begin
  4.   SetLength(where, 0);
  5.   ShowMessage(IntToStr(Length(where)));
or:
Code: Pascal  [Select][+][-]
  1. var
  2.   where: array of String;
  3. begin
  4.   SetLength(where, 0);
  5.   ShowMessage(Length(where).ToString);
Specialize a type, not a var.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Dynamic arrays in pascal?
« Reply #2 on: August 05, 2018, 03:38:36 pm »
I have this code:
Code: Pascal  [Select][+][-]
  1.  
  2. var
  3.   where: array of String;
  4. begin
  5.   SetLength(where, 0);
  6.   ShowMessage(String(Length(where)));

...just shows an empty messagebox... :o
While it should show the length of the array where, that is 0.
What do I do wrong?
do you know the difference between casting and converting?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Dynamic arrays in pascal?
« Reply #3 on: August 05, 2018, 04:12:46 pm »
do you know the difference between casting and converting?
Do you know how to add example code? :D O:-)
Specialize a type, not a var.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Dynamic arrays in pascal?
« Reply #4 on: August 05, 2018, 04:18:40 pm »
do you know the difference between casting and converting?
Do you know how to add example code? :D O:-)
sure, but I see no problems in your source, I see no reason to extend upon it.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Dynamic arrays in pascal?
« Reply #5 on: August 05, 2018, 10:19:17 pm »
I have this code:
Code: Pascal  [Select][+][-]
  1.  
  2. var
  3.   where: array of String;
  4. begin
  5.   SetLength(where, 0);
  6.   ShowMessage(String(Length(where)));

...just shows an empty messagebox... :o
While it should show the length of the array where, that is 0.
What do I do wrong?
do you know the difference between casting and converting?
Yes, but I was based on an example of yours:
Code: Pascal  [Select][+][-]
  1. showMessage('You selected the item with Code :'+string(DBLookupComboBox1.keyvalue));
...and I thought that things in pascal are different.

Thanks, the '.ToString' conversion works!
« Last Edit: August 05, 2018, 10:44:42 pm by panoss »
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Dynamic arrays in pascal?
« Reply #6 on: August 06, 2018, 03:24:54 am »
I have this code:
Code: Pascal  [Select][+][-]
  1.  
  2. var
  3.   where: array of String;
  4. begin
  5.   SetLength(where, 0);
  6.   ShowMessage(String(Length(where)));

...just shows an empty messagebox... :o
While it should show the length of the array where, that is 0.
What do I do wrong?
do you know the difference between casting and converting?
Yes, but I was based on an example of yours:

Code: Pascal  [Select][+][-]
  1. showMessage('You selected the item with Code :'+string(DBLookupComboBox1.keyvalue));
...and I thought that things in pascal are different.
variants are different. they auto convert to what ever type you need I use cast as a mechanism to determine the exact target type although mostly unneeded in the above case some times auto convertion does an extra step that might or might not raise problems.
Thanks, the '.ToString' conversion works!
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018