Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Free Pascal shifty parameter types

2 Posts
2 Users
0 Reactions
2,109 Views
(@woodybrison)
New Member
Joined: 10 years ago
Posts: 1
Topic starter  

I have several procedures that operate on strings.

Some of my strings are shortstrings and some are Ansistrings. It doesn't make a lot of sense to make them all Ansistrings.

I want to pass these strings in to my procedures, and have the procedures deal with them regardless of which type they are.

Naturally, FPC gives me error messages if the types don't match.

My only solution so far is to make two versions of each procedure, one that works on shortstrings and one YCFS*

Isn't there some construct that looks like a case statement?

*YCFS = you can finish the sentence


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Hi WoodyBrison,

I'm not sure if you're working with just Free Pascal or with Lazarus and Free Pascal ... 

Anyhow ... I've tried the following in Lazarus Pascal and it just works. Could you illustrate with some code what you're running into?

Code below is a simple Form, with a TLabel and 2 Buttons ...

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
  { TForm1 }
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    procedure MixStrings(MyString:AnsiString);
  end;
var
  Form1: TForm1;
implementation
{$R *.lfm}
procedure TForm1.Button1Click(Sender: TObject);
var ShortStr:ShortString;
begin
  ShortStr:='Test Shortstring';
  MixStrings(ShortStr);
end;
procedure TForm1.Button2Click(Sender: TObject);
var AnsiStr:AnsiString;
begin
  AnsiStr:='Test AnsiString';
  MixStrings(AnsiStr);
end;
procedure TForm1.MixStrings(MyString:AnsiString);
begin
  Label1.Caption:=MyString;
end;
end.

As you can see, the procedure "MixStrings" takes an AnsiString but when passing a shortstring it just works as well.

This could be related to the compiler directive {$H+} which is added in Lazarus by default.
(see also: AnsiStrings Documentation)


   
ReplyQuote
Share: