从ShortString到String的隐式字符串转换

时间:2014-08-19 18:43:04

标签: delphi implicit-cast

伙计我正在使用Delphi XE3,我遇到了这个错误,我试图查看以前的问题,并尝试相应地解决,但错误不会消失。任何使用过XE3的人请帮助我,我很沮丧。至于编程新手

[dcc32警告] invoice_u.pas(92):W1057从'ShortString'到'string'的隐式字符串

以下是我的代码

procedure TfrmInvoice.btnCreateClick(Sender: TObject);
begin
dmoSales.cdsInvoice.Insert; // insert a new record

dmoSales.cdsInvoice.FieldByName('InvNumber').AsString := invoiceNumber;
dmoSales.cdsInvoice.FieldByName('CustNumber').AsString :=
dmoSales.cdsCustomer.FieldByName('CustNumber').AsString;
dmoSales.cdsInvoice.FieldByName('InvDate').AsDateTime := Date;
dmoSales.cdsInvoice.FieldByName('InvPaid').AsBoolean := chkInvPaid.Checked;

dmoSales.cdsInvoice.Post; // save new record
end;

procedure TfrmInvoice.FormClose(Sender: TObject; var Action: TCloseAction);
begin
dmoSales.cdsCustomer.Close;
if dmoSales.cdsInvoice.ChangeCount > 0 then
 dmoSales.cdsInvoice.ApplyUpdates(0);
 dmoSales.cdsInvoice.Close;

end;

procedure TfrmInvoice.FormShow(Sender: TObject);
var
numberOfRecords : integer;
todaysDate : TDateTime;

begin
dmoSales.cdsInvoice.Open;
//get todays date so that the year can be extracted
//add DateUtils to uses clause for YearOf function
todaysDate := Today;
invoiceNumber:= IntToStr(YearOf(todaysDate));
//get the current number of invoices so that it can be added to invoiceNumber
numberOfRecords := dmoSales.cdsInvoice.RecordCount + 1;

//create invoice number
case numberOfRecords  of
1..9:  invoiceNumber := invoiceNumber + '000' + IntToStr(numberOfRecords);
10.99: invoiceNumber := invoiceNumber + '00' + IntToStr(numberOfRecords);
100..999: invoiceNumber := invoiceNumber + '0' + IntToStr(numberOfRecords);
else
     invoiceNumber := invoiceNumber + IntToStr(numberOfRecords);
end;

//display invoice number on form
lblInvNumber.Caption := invoiceNumber;

//get the date and display it on the form
lblDate.Caption := DateToStr(Date);


// get the customers into alphabetic order (using an SQL query)
dmoSales.cdsCustomer.Close; // close the dataset before making changes
dmoSales.sdsCustomer.CommandType := ctQuery; // set dataset up as a query
dmoSales.sdsCustomer.CommandText := 'SELECT*FROM Customer ORDER BY CustName';
dmoSales.cdsCustomer.Open;
dmoSales.cdsCustomer.Refresh;


end;

端。

1 个答案:

答案 0 :(得分:1)

我们无法分辨出警告适用于哪一行,也无法看到代码中所有变量的类型。但是,警告信息足够清楚。你做的某个地方

s := t;

其中sstringUnicodeString的别名,tShortString。由于ShortString始终是ANSI编码的,因此编译器会警告您正在进行从ANSI到UTF-16的隐式转换。它警告你,因为你应该知道这种转换,并且是隐含的,通过阅读代码并不是很明显。

解决问题的最佳方法是停止使用已弃用的长ShortString类型。今天真的没有地方。

如果你无法做到这一点,那么你可以通过明确的转换来抑制警告。

s := string(t);

当然,这会激发显式字符串强制转换警告W1059,但默认情况下该警告已关闭。