Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/Errors - Warnings    [ Add a report in this area ]  
Report #:  118866   Status: Closed
Cannot perform FillChar on Currency variables -  E2197 Constant object cannot be passed as var parameter
Project:  Delphi Build #:  18.0.4905.60485
Version:    18.1 Submitted By:   Jeroen Pluimers
Report Type:  Basic functionality failure Date Reported:  9/10/2013 10:18:24 AM
Severity:    Infrequently encountered problem Last Updated: 12/10/2013 6:29:33 PM
Platform:    All platforms Internal Tracking #:   43528
Resolution: Fixed (Resolution Comments) Resolved in Build: : XE5 Update2
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description
Cannot perform FillChar on Currency variables
E2197 Constant object cannot be passed as var parameter

See steps: the project there does not compile.

This is a simplification of QC#87168 (which is marked as 'need feedback')
Steps to Reproduce:
Compile this program.
----------------
program FillCharCurrencyVarFailsWithE2197;

uses
  Windows;

{$if CompilerVersion >= 20} // Delphi 2009 introduced Generics with Default initialization
procedure DefaultTest;
var
  DoubleVar: Double;
  CurrencyVar: Currency;
begin
  DoubleVar := Default(Double);
  CurrencyVar := Default(Currency);
end;
{$endif CompilerVersion >= 20} // Delphi 2009 introduced Generics with Default initialization

procedure ZeroMemoryTest;
var
  DoubleVar: Double;
  CurrencyVar: Currency;
begin
  ZeroMemory(@DoubleVar, SizeOf(DoubleVar));
  ZeroMemory(@CurrencyVar, SizeOf(CurrencyVar));
end;

procedure FillCharTest;
var
  DoubleVar: Double;
  CurrencyVar: Currency;
begin
  FillChar(DoubleVar, SizeOf(DoubleVar), 0);
// compiler error: E2197 Constant object cannot be passed as var parameter
  FillChar(CurrencyVar, SizeOf(CurrencyVar), 0);
end;

// http://stackoverflow.com/questions/18717991/why-is-a-currency-variable-treated-as-a-constant-with-fillchar-in-delphi
// also fails with just this as the main program:
var
  DoubleVar: Double;
  CurrencyVar: Currency;
begin
  FillChar(DoubleVar, SizeOf(DoubleVar), 0);
// compiler error: E2197 Constant object cannot be passed as var parameter
  FillChar(CurrencyVar, SizeOf(CurrencyVar), 0);
end.
----------------
Workarounds
Use ZeroMemory or FillMemory (for any DelphiVersion) or Default(T) for Delphi Versions >= 2009.
Attachment
JEDDCA2.zip
Comments

None

Server Response from: ETNACODE01