为什么Delphi在将ShortString分配给字符串时会发出警告? [英] Why does Delphi warn when assigning ShortString to string?

查看:261
本文介绍了为什么Delphi在将ShortString分配给字符串时会发出警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有一些旧的ShortStrings,如字符串[25]



为什么下面的作业:

 键入
S:String;
ShortS:String [25];

...
S:= ShortS;

导致编译器生成此警告:

  W1057从ShortString到string的隐式字符串转换。 

这里没有发生数据丢失。在什么情况下这个警告对我有帮助的信息?



谢谢!



Tomw

解决方案

ShortString类型没有更改。事实上,它仍然是一系列的AnsiChar。



通过将其分配给字符串类型,您将使用一组AnsiChars(一个字节)并将其放入一组WideChars(两个字节)。编译器可以做到这一点很好,而且足够聪明,不会丢失数据,但警告是让您知道这种转换已经发生。


I'm converting some legacy code to Delphi 2010.

There are a fair number of old ShortStrings, like string[25]

Why does the assignment below:

type 
  S: String;
  ShortS: String[25];

...
S := ShortS;

cause the compiler to generate this warning:

W1057 Implicit string cast from 'ShortString' to 'string'.

There's no data loss that is occurring here. In what circumstances would this warning be helpful information to me?

Thanks!

Tomw

解决方案

The ShortString type has not changed. It continues to be, in effect, an array of AnsiChar.

By assigning it to a string type, you are taking what is a group of AnsiChars (one byte) and putting it into a group of WideChars (two bytes). The compiler can do that just fine, and is smart enough not to lose data, but the warning is there to let you know that such a conversion has taken place.

这篇关于为什么Delphi在将ShortString分配给字符串时会发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆