Jump to content
Bart Verbakel

W1057 during D7 -> D11 conversion

Recommended Posts

Hello,

 

I am working for 20 years on a Delphi application, just updating it every year to meet my requirements and remove bugs.

I have been using Delphi 7 until now, but lately I tried to update 20 years of code to Delphi 11.

Some code has to be changed, and I found a solution for this changes.

But somehow I am struggling with W1057 Implicit string cast from 'ShortString' to 'string' warnings.

I know, this are only warning and I can disable them in the compiler settings, but I prefer a better solution.

 

The code:

 MessageDlg(F1Pool.Deelnemer[High(F1Pool.Deelnemer)].Teamnaam + 'succesfully added!',mtInformation,[mbOk],0);

 

Where F1Pool.Deelnemer.Teamnaam is declared as String[100];

 

It works OK(without warnings) then I only use substring 'succesfully add',but when I combine a string from a record with a fixed string I got warning W1057.

 

I tried to use stringconversions like String(F1Pool.Deelnemer[High(F1Pool.Deelnemer)].Teamnaam), but all in vain...

 

What am I doing wrong? I used this code in Delphi 7 for years without warnings.

 

Bart

 

 

Share this post


Link to post

@Bart Verbakel

 

did you see this:  https://docwiki.embarcadero.com/RADStudio/Alexandria/en/W1057_Implicit_string_cast_from_'%s'_to_'%s'_(Delphi)

<your_target_string> := string(<your_ansi_source); // explicitly before usage, avoid implicity convertion

NOTE: See too https://docwiki.embarcadero.com/RADStudio/Alexandria/en/W1059_Explicit_string_cast_from_'%s'_to_'%s'_(Delphi)   (Explictiy: warning )

 

As rule: avoid ShortString in new IDE! or use "compiler directives" {$IFDEF VER350} xxxx {$ELSE} wwww {$ENDIF}

https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Compiler_Versions

https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Conditional_compilation_(Delphi)

Edited by programmerdelphi2k

Share this post


Link to post

I am using ShortString because I have to save the data to an external file. I think (correct me if I am wrong) this is only possible with an ShortString.

 

 

Share this post


Link to post

" Why are you using shortstring? Even in D7 that was not recommended.  "

 

I have to save the data to an external file. What do you recommend in this case?

I thought declaring a variable a String, it could take up (almost) unlimited space, so therefore it is not possbile to write it to a file

 

Bart

 

Share this post


Link to post
14 minutes ago, Bart Verbakel said:

I am using ShortString because I have to save the data to an external file. I think (correct me if I am wrong) this is only possible with an ShortString.

 

 

No. Its perfectly possible to save any string type to a file. 

Share this post


Link to post

It is a record, build up with various datatypes. (Limited) strings, integers, booleans and reals.

So it is not a simple textfile

 

Bart

 

Share this post


Link to post

Blitting a record to a binary file makes it more tricky. Nobody but you can really know what to do. Perhaps you just carry on and suppress the warnings after localising them. But why go to Delphi 11 then? If you carry on as you are you can't do Unicode. Maybe you don't need international language support. Maybe you don't mind having pre determined max text lengths. But if you want to go beyond these limitations it's going need some thought and design. 

Share this post


Link to post

@Bart Verbakel Is it a file used only by your application(s) - i.e. are you in control of production and consumption of the file?

In that case, you have the alternative to modernize the file structure to f.x. JSON to support unicode and additional/optional fields.

 

If you stay on your Record format, you need to do explicit conversions between your shortstrings and other strings in the system.

 

MessageDlg(

 String( F1Pool.Deelnemer[High(F1Pool.Deelnemer)].Teamnaam )

 + ' succesfully added!', mtInformation, [mbOk], 0);  // Should silence the W1057

Share this post


Link to post

Yes, I have to freedom to change the data structure.

My first goal was to get the 20-year old source code working on D11 without any errors. That goal is already reached. Now I can focus on optimizing the source code to modern standards.

I have to do some research to bridge this 20-year gap 😅

 

Bart

 

  • Like 3

Share this post


Link to post
5 hours ago, Bart Verbakel said:

Yes, I have to freedom to change the data structure.

Is this a kind of database for the application data?

Share this post


Link to post

No, every year I set up a F1 manager game, where competitors have to buy a F1 team and earn points and money.

This application calculates all the points during the F1 season. It is modified year after year to suit the needs. But now I want to switch from D7 to D11.

 

Bart

 

Share this post


Link to post

I don't HAVE to go to Delphi 11, but I have 2 reasons for making this choice:

 

1. Delphi 7 is not supported by Windows 11. I need an old laptop running in Windows 7 and Delhi 7 to write the application.

2. I want to learn and improve my Application, to give it a modern look and doný stick to 20 years old software if an update is available.

 

If it is too difficult or time consuming I will stick to Delphi 7. But I will give it a try.

 

On 6/23/2023 at 9:16 AM, David Heffernan said:

Blitting a record to a binary file makes it more tricky. Nobody but you can really know what to do. Perhaps you just carry on and suppress the warnings after localising them. But why go to Delphi 11 then? If you carry on as you are you can't do Unicode. Maybe you don't need international language support. Maybe you don't mind having pre determined max text lengths. But if you want to go beyond these limitations it's going need some thought and design. 

 

Share this post


Link to post
1 hour ago, Bart Verbakel said:

Delphi 7 is not supported by Windows 11. I need an old laptop running in Windows 7 and Delhi 7 to write the application.

Really. I'm pretty sure you can run Delphi 7 on Win 11. 

 

 

Share this post


Link to post
4 minutes ago, David Heffernan said:

Really. I'm pretty sure you can run Delphi 7 on Win 11. 

 

 

I got compatibility warnings during installation, so I decided to use Windows 7. But I can try it on Windows 11.

Share this post


Link to post
8 minutes ago, Bart Verbakel said:

I got compatibility warnings during installation, so I decided to use Windows 7. But I can try it on Windows 11.

Yeah. You just ignore those and generally install to a write able directory, or put a permissive acl on the installation directory. It's been that way for many windows versions too. 

Share this post


Link to post
12 minutes ago, Fr0sT.Brutal said:

I have apps built with D7 on XP running OK on W10

That's not what we are talking about. We are talking about running the ide on Windows 11.

Share this post


Link to post
4 minutes ago, David Heffernan said:

That's not what we are talking about. We are talking about running the ide on Windows 11.

I know that. But OP wants the app to be modern, he doesn't require the dev machine be modern.

I'm saying that even very old Windows + Delphi produce apps running fine on modern Windows. So using modern IDE on W10 easily allows to produce apps running on W11

Edited by Fr0sT.Brutal

Share this post


Link to post
8 minutes ago, Fr0sT.Brutal said:

I know that. But OP wants the app to be modern, he doesn't require the dev machine be modern.

I'm saying that even very old Windows + Delphi produce apps running fine on modern Windows. So using modern IDE on W10 easily allows to produce apps running on W11

OP doesn't want to run multiple machines. It's just one dev that wants to get rid of ancient Win7 laptop. 

 

My point is that you can just install D7 on Win 11. If that is helpful. 

Share this post


Link to post

I am now experimenting with JSON datafiles for a week now, and I think I get a better understanding of how JSON files work.

It is quite simple, if you know the correct procedures for serialising/deserialising my records. I only should gain some confidence in the conversion...

It took a while to find out that serialsing a record with String[x] does not work, properly but does not return in an error either. It simply does not serialise that variable, resulting in data loss.

After changing my strings to a string without a predefined length everything is OK.

 

Thanks for the help so far!

Share this post


Link to post

Hello all,

 

Another question...

 

I want to include some error handling in case a JSON string does not match the data structure.

I tried the following code:

 

var Serializer:TJsonSerializer;

var Person:TPerson;

begin

  Serializer:=TJsonSerializer.create;

  try

    Person:=Serializer.Deserialize<TPerson>(JSON_inputstring);

  except

    Showmessage('Error in JSON string');

  end;

  FreeAndNil(Serializer);

end;

 

For a correct JSON string, this code works OK,

but this does not work in case the inputstring has an invalid format (eg. 'ABCD')

 

How can I handle these kind of errors?

 

Bart

 

 

Share this post


Link to post

a simple parse can help you... and you dont need raise a exception!

procedure TForm1.Button1Click(Sender: TObject);
var
  LJSONValue: TJSONValue;
begin
  try
    // '', use bool, raise Exception params
    LJSONValue := TJSONValue.ParseJSONValue('[', true, false); // any string: empty, valid, invalid
    try
      if (LJSONValue <> nil) then
        Memo1.Text := LJSONValue.ToJSON
      else
        Memo1.Text := 'JSON nil';
    finally
      LJSONValue.Free;
    end;
  except
    on E: Exception do
      ShowMessage('JSON invalid: ' + E.Message);
  end;
end;
Edited by programmerdelphi2k

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×