Recent

Author Topic: [SOLVED] Type conversion?  (Read 1533 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1266
[SOLVED] Type conversion?
« on: August 15, 2022, 12:19:40 pm »
Hi, why following codes causes compile error?

Code: Pascal  [Select][+][-]
  1. type
  2.    TInputTypes = (itText, itPassWord, itRadio, itCheckbox, itHidden, itTel,
  3.                  itNumber, itUrl, itEmail, itRrange, itDate, itMonth, itWeek);
  4.  
  5. var
  6.    ts: string;
  7.    anIT: TInputTypes;
  8.  
  9. begin
  10.    ts := // sometext;
  11.    ReadStr(ts, anIT);  <== this is not compiled ...  
  12. end;
  13.  

Error message : Typecast has different size (1 -> 4) in assignment
« Last Edit: August 15, 2022, 01:47:24 pm by egsuh »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Type conversion?
« Reply #1 on: August 15, 2022, 12:28:08 pm »
The documentation isn't exactly helpful, but I think the big question is what types (as the non-first argument(s)) ReadStr() accepts: it might be unreasonable to expect it to handle a locally-defined enumeration.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Type conversion?
« Reply #2 on: August 15, 2022, 12:34:26 pm »
What Mark said.
Added: He's passing the Enum-Variable, and not an Enum-Value to ReadStr.....

What exactly is OP trying to achieve?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Type conversion?
« Reply #3 on: August 15, 2022, 12:44:21 pm »
What Mark said.
Added: He's passing the Enum-Variable, and not an Enum-Value to ReadStr.....

What exactly is OP trying to achieve?

I assume that he's trying to read into the variable. Might be doable with a cast.

I suspect that it would be tricky to override ReadStr(), even for a fixed number of parameters.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dje

  • Full Member
  • ***
  • Posts: 134
Re: Type conversion?
« Reply #4 on: August 15, 2022, 12:46:20 pm »
You seem to want a FPC feature in DELPHI mode (or maybe even in Delphi). The following code works in FPC mode, but produces your error in DELPHI mode.

Maybe someone else can provide the exact reason why.

Code: Pascal  [Select][+][-]
  1. {$mode fpc}
  2. program project1;
  3. type
  4.   TInputTypes = (itText, itPassWord, itRadio, itCheckbox, itHidden, itTel,
  5.     itNumber, itUrl, itEmail, itRrange, itDate, itMonth, itWeek);
  6. var
  7.   ts: string;
  8.   anIT: TInputTypes;
  9. begin
  10.   ts := 'itText';
  11.   ReadStr(ts, anIT);
  12.   Readln;
  13. end.  
« Last Edit: August 15, 2022, 12:49:49 pm by derek.john.evans »

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Type conversion?
« Reply #5 on: August 15, 2022, 12:54:02 pm »
This not making any sense.
Acc. to documentation, ReadStr works like Read, but without a File.
In this case ts being the "File", and anIT the "Target"
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dje

  • Full Member
  • ***
  • Posts: 134
Re: Type conversion?
« Reply #6 on: August 15, 2022, 12:57:41 pm »
Packenum seems to fix it. Maybe look into that.

Code: Pascal  [Select][+][-]
  1. {$PACKENUM 4}

https://www.freepascal.org/docs-html/prog/progsu59.html
Quote
The default enumeration size depends on the compiler mode:

    In Delphi and TP mode, the size is 1.
    In MacPas mode, the size is 2.
    In all other modes, the default is 4.
« Last Edit: August 15, 2022, 01:00:38 pm by derek.john.evans »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Type conversion?
« Reply #7 on: August 15, 2022, 01:03:37 pm »
This not making any sense.
Acc. to documentation, ReadStr works like Read, but without a File.
In this case ts being the "File", and anIT the "Target"

The only useful bit in the documentation is the example, which shows a string being written to a temporary textfile using Write() and then parsed into multiple elements using Read(). For some small value of "useful" :-/

So the first parameter of ReadStr() is a (constant) string, and the non-first are values of types acceptable to the RTL to be parsed out of it. In theory, at least :-/

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

egsuh

  • Hero Member
  • *****
  • Posts: 1266
Re: Type conversion?
« Reply #8 on: August 15, 2022, 01:04:54 pm »
Thank you for your advices.  This compiles in {$mode objfpc}.
Haven't tested running yet. I'll post the result.

egsuh

  • Hero Member
  • *****
  • Posts: 1266
Re: Type conversion?
« Reply #9 on: August 15, 2022, 01:47:08 pm »
I have tested this which works as expected.  To add some more explanation, I'm trying this to save the whole option as TStringList.  As you can see in the following codes, one CodeOption corresponds to an Input of HTML Dom. I'd like to define HTML inputs programmatically, and users are able to edit the attributes of the inputs in my Lazarus program, using TIPropertyGrid.

The content is saved as text, TCodeOptions.Text, in DB.  I'm trying to reduce my own code writings.


Code: Pascal  [Select][+][-]
  1. type
  2.   TInputTypes = (itText, itPassWord, itRadio, itCheckbox, itHidden, itTel,
  3.                  itNumber, itUrl, itEmail, itRange, itDate, itMonth, itWeek);
  4.  
  5.   TCodeOptions = class (TStringList)
  6.   private
  7.      function getInputType: TInputTypes;
  8.      function getIntegerOption(AIndex: Integer): integer;
  9.      function getRequired: Boolean;
  10.      function getStringOption(AIndex: Integer): string;
  11.      procedure setInputType(AValue: TInputTypes);
  12.      procedure setIntegerOption(AIndex: Integer; AValue: integer);
  13.      procedure setRequired(AValue: Boolean);
  14.      procedure setStringOption(AIndex: Integer; AValue: string);
  15.   published
  16.      // HTML input attributes
  17.      property InputType: TInputTypes read getInputType write setInputType;
  18.      property Pattern: string     index 2 read getStringOption write setStringOption;
  19.      property PlaceHolder: string index 3 read getStringOption write setStringOption;
  20.      property Title: string       index 4 read getStringOption write setStringOption;
  21.  
  22.      property Required: Boolean read getRequired write setRequired;
  23.      property MinLength: integer index 1 read getIntegerOption write setIntegerOption;
  24.  
  25.      property Min: integer index 11 read getIntegerOption write setIntegerOption;
  26.      property Max: integer index 12 read getIntegerOption write setIntegerOption;
  27.      property Step: integer index 13 read getIntegerOption write setIntegerOption;
  28.  
  29.      // Custom options
  30.      property PreText: string index 101 read getStringOption write setStringOption;
  31.      property PostText: string index 102 read getStringOption write setStringOption;
  32.   end;
  33.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: [SOLVED] Type conversion?
« Reply #10 on: August 15, 2022, 02:03:24 pm »
So what are you actually seeing stored: a binary value, or a text representation? If a text representation, is there a robust delimiter?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

egsuh

  • Hero Member
  • *****
  • Posts: 1266
Re: [SOLVED] Type conversion?
« Reply #11 on: August 15, 2022, 05:39:23 pm »
The result is text string. Delimiter is #13#10.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: [SOLVED] Type conversion?
« Reply #12 on: August 15, 2022, 06:02:55 pm »
I was assuming that you were putting more than one argument in WriteStr() or ReadStr()... have you checked what happens in that case?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

egsuh

  • Hero Member
  • *****
  • Posts: 1266
Re: [SOLVED] Type conversion?
« Reply #13 on: August 16, 2022, 04:51:00 am »
@MarkMLI

As I have shown in my example, I want ReadStr and WriteStr to read/write an enumerated type from/to string, so that I can save the content as text. So I was not interested in multi-arguments in ReadStr and WriteStr. But your comment attracted my interested and tested it, which returns what are expected.  Following example displays expected string.


Code: Pascal  [Select][+][-]
  1. type
  2.   TDays = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);    
  3.  
  4.  
  5. implementation
  6.  
  7. procedure TfCodeOption.Button4Click(Sender: TObject);
  8. var
  9.    Day1, Day2: TDays;
  10.    ts, s1, s2, s3: string;
  11.    i, j, k: integer;
  12. begin
  13.    ts := 'tuesday 3 sunday ''this is a book''';
  14.    ReadStr(ts, Day1, i, Day2, s1);
  15.    WriteStr(s2, Day1);
  16.    WriteStr(s3, Day2);
  17.    WriteStr(ts, i, ', ', s1, ', ', Day1, ', ', Day2);
  18.    showmessage(ts);  // this returns -->   3, 'this is a book', Tuesday, Sunday
  19. end;
  20.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: [SOLVED] Type conversion?
« Reply #14 on: August 16, 2022, 09:04:47 am »
That obviously followed on from my original question about whether the result was binary or text.

So if you assembled a string using

Code: Pascal  [Select][+][-]
  1. WriteStr(ts, 1, 2, 3);
  2.  

ReadString() won't reliably parse it.

MarkMLl


MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018