加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi – W1048对’TFormatSettings’的’string’的不安全类型

发布时间:2020-12-15 09:18:18 所属栏目:大数据 来源:网络整理
导读:我正在将一个旧项目移植到Delphi XE,我在下面的代码中收到此警告. function RemoveThousandSeperator(Text: String) : String;Var P : Integer;begin if length(Text) 3 then begin p := Pos(FormatSettings.ThousandSeparator,Text); while p 0 do begin De
我正在将一个旧项目移植到Delphi XE,我在下面的代码中收到此警告.

function RemoveThousandSeperator(Text: String) : String;
Var P : Integer;
begin
  if length(Text) > 3 then begin
    p := Pos(FormatSettings.ThousandSeparator,Text);
   while p >0 do begin
      Delete(Text,p,1);
      p := Pos(FormatSettings.ThousandSeparator,Text);
    end;
  end;
  result := Text;
end;

甚至FormatSettings.ThousandSeparator的类型为char.

LE:我问是否有人可以告诉我为什么会出现此警告.代码很旧,它将被重新制作.

LE2:为了得到这个警告,所有警告都需要在Delphi Compiler-Hints&中设置为true.警告

LE3:如果有人需要它 – {$WARN UNSAFE_CAST OFF}使警告响起.

LE4:对那些认为警告难以置信的人的警告截图

解决方法

警告的起源是SysUtils.pas中FormatSettings变量的声明:

var
  // Note: Using the global FormatSettings variable corresponds to using the
  // individual global formatting variables and is not thread-safe.
  FormatSettings: TFormatSettings absolute CurrencyString;

将字符串(CurrencyString)转换为记录(TFormatSettings).

因此,生成警告的问题是在SysUtils.pas中,而不是在您发布的代码中,
虽然警告是在您的代码中生成的.

这是一个测试用例(Delphi XE):

program Project1;

{$APPTYPE CONSOLE}
{$WARN UNSAFE_CAST ON}

type
  TTest = record
    FS: string;
  end;

var
  Str: string;
  Test: TTest absolute Str;

begin
  Str:= 'abc';
  Writeln(Test.FS);   //  W1048 Unsafe typecast of 'string' to 'TTest'
end.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读