常数“基数数组”产生错误“常数表达式违反了子范围边界”

时间:2019-02-23 07:38:59

标签: delphi compiler-errors delphi-xe4

为什么要输入以下代码:

  CreateRole(regForm: NgForm) {

    if (regForm.value.Id == null) {

      this.objRoleModel.RoleName = regForm.value.roleName;
      this.objRoleModel.IsActive = regForm.value.isActive;

      this.roleService.CreateRole(this.objRoleModel).subscribe(res => {
        console.log(this.objRoleModel);
        alert("Role Added Successfully !!");
      })
    }
    else {

      this.roleService.updateRole(this.objRoleModel).subscribe(res => {
        alert("Role Updated Successfully !!");
      });
    }

  }

在Delphi XE4(win32和win64)下都会产生以下编译错误:

  

[dcc32错误] Debug.pas(66):E1012常数表达式违反了子范围边界

import { Injectable } from '@angular/core'; import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http'; import { Observable, throwError } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; import { RoleModel } from "../models/role.model"; @Injectable({ providedIn: 'root' }) export class RoleService { objRoleModel: RoleModel; constructor(private http: HttpClient) { this.objRoleModel = new RoleModel(); } GetRoleById(Id): Observable<any> { debugger; return this.http.get("https://localhost:44336/api/Roles/" + Id) .pipe( map((response: Response) => { return response; }), catchError((error: Response) => { return throwError(console.log(error)); }) ); } CreateRole(objRoleModel: RoleModel) { debugger; const headers = new HttpHeaders().set('content-type', 'application/json'); var body = { Id: objRoleModel.Id, RoleName: objRoleModel.RoleName, IsActive: objRoleModel.IsActive } return this.http.post<RoleModel>("https://localhost:44336/api/Roles", body, { headers }); } updateRole(objRoleModel: RoleModel) { debugger; const params = new HttpParams().set("Id", objRoleModel.Id.toString()); const headers = new HttpHeaders().set('content-type', 'application/json'); var body = { Id: objRoleModel.Id, RoleName: objRoleModel.RoleName, IsActive: objRoleModel.IsActive } return this.http.put<RoleModel>("https://localhost:44336/api/Roles?" + objRoleModel.Id, body, { headers, params }) } } 是否在const ANSICOLORS: array of cardinal = [ $000000,//0 $800000,//1, compilation error starts with this value $008000,//2 $808000,//3 $000080,//4 $800080,//5 $008080,//6 $D0D0D0,//7 $3F3F3F,//8 $FF0000,//9 $00FF00,//A $FFFF00,//B $0000FF,//C $FF00FF,//D $00FFFF,//E $FFFFFF];//F 的范围内?

2 个答案:

答案 0 :(得分:8)

您正在使用XE7中引入的功能。

声明动态数组常量。

Dynamic arrays_Delphi xe7

String-Like_Operations_Supported_on_Dynamic_Arrays


在XE4中,它被评估为一组,其序数不能超过255。

出现错误E1012 Constant expression violates subrange bounds

通过声明解决此问题:

const
  ANSICOLORS: array[0..15] of cardinal = (
    $000000,//0
    $800000,//1, 
    $008000,//2
    $808000,//3
    $000080,//4
    $800080,//5
    $008080,//6
    $D0D0D0,//7
    $3F3F3F,//8
    $FF0000,//9
    $00FF00,//A
    $FFFF00,//B
    $0000FF,//C
    $FF00FF,//D
    $00FFFF,//E
    $FFFFFF);//F

答案 1 :(得分:4)

从内存角度来看,$800000确实在Cardinal的范围内。

我认为Cardinal($800000)类型转换可以解决该问题,但不能解决。

Delphi XE7及更高版本实际上可以正确编译代码。

这也会失败,直到出现相同错误的Delphi XE6:

const
  CardinalArray: array of Cardinal = [257];

我认为原因是Delphi编译器将[257]视为set,尽管左侧表明它应该是array

这在XE4中失败,但在XE7及更高版本中可以正常编译:

program Cardinals;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

const
  CardinalArray: array of Cardinal = [257]; // fails until Delphi XE4 with "E1012 Constant expression violates subrange bounds"

const
  ANSICOLORS: array of Cardinal = [
    $000000,//0
    $800000,//1, compilation error starts with this value
    $008000,//2
    $808000,//3
    $000080,//4
    $800080,//5
    $008080,//6
    $D0D0D0,//7
    $3F3F3F,//8
    $FF0000,//9
    $00FF00,//A
    $FFFF00,//B
    $0000FF,//C
    $FF00FF,//D
    $00FFFF,//E
    $FFFFFF];//F

var
  AnsiColor: Cardinal;

begin
  try
    for AnsiColor in AnsiColors do
      Writeln(Format('$%6.6x', [AnsiColor]));
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

输出:

$000000
$800000
$008000
$808000
$000080
$800080
$008080
$D0D0D0
$3F3F3F
$FF0000
$00FF00
$FFFF00
$0000FF
$FF00FF
$00FFFF
$FFFFFF

如果可以使用非动态数组,则以下示例可在Delphi XE4中使用(我早在Delphi 2007上就对其进行了测试,对于XE和更早的版本,必须将System.SysUtils替换为{{ 1}})。

请注意开关:

  • SysUtilsarray of Cardinal
  • array[0..15] of Cardinal[](
)