![]() |
Delphi-Version: 2010
Unterschiede zwischen Konstanten-Deklaration?
Hallo,
Ich habe eine Unit, welche viele Kontanten in dieser Form enthaelt:
Delphi-Quellcode:
Nun will ich in einer anderen Unit diese fuer Case-Abfragen nutzen:
const
{ Amp Drive } CTRL_DRIVE: Byte = $0D; // $00 to $7E { Amp Toogle Controls (avail. depends on model!) } CTRL_DRIVE_ONOFF: Byte = $1A; // $00 (off), $40 (on) CTRL_DIST_ONOFF: Byte = $19; // $00 (off), $40 (on)
Delphi-Quellcode:
ControlID ist vom Typ Byte. Allerdings meckert jetzt Delphi rum, das eine Konstante fuer die Case-Faelle benoetigt wird ("[DCC Error] fmMain.pas(134): E2026 Constant expression expected"). Definiere ich die Konstanten ohne expliziten Typ z.B. mit
case ControlID of
CTRL_DRIVE: DoSomething(); CTRL_EQ_BASS: DoSomething(); CTRL_EQ_MID: DoSomething(); CTRL_EQ_HIGH: DoSomething(); CTRL_CHANNEL_VOLUME: DoSomething(); end;
Delphi-Quellcode:
, so geht es. Allerdings erschliest sich mir nicht der Unterschied (sind ja beides Konstanten...).
CTRL_DRIVE = $0D;
Kann ich das ": Byte" ruhigen Gewissens weglassen und Delphi macht trotzdem ein Byte draus (wegen Bit-Operatoren)? |
AW: Unterschiede zwischen Konstanten-Deklaration?
Zitat:
sondern nur schreibgeschützte Variablen. CASE verlangt aber "echte" Konstanten. Dieses sind "echte" Konstanten:
Code:
oder
const
{ Amp Drive } CTRL_DRIVE = $0D; // $00 to $7E { Amp Toogle Controls (avail. depends on model!) } CTRL_DRIVE_ONOFF = $1A; // $00 (off), $40 (on) CTRL_DIST_ONOFF = $19; // $00 (off), $40 (on)
Code:
const
{ Amp Drive } CTRL_DRIVE = Byte($0D); // $00 to $7E { Amp Toogle Controls (avail. depends on model!) } CTRL_DRIVE_ONOFF = Byte($1A); // $00 (off), $40 (on) CTRL_DIST_ONOFF = Byte($19); // $00 (off), $40 (on) |
AW: Unterschiede zwischen Konstanten-Deklaration?
Danke fuer die Aufklaerung :)
Will nur kurz nachhaken:
Delphi-Quellcode:
ist dann bestimmt auch nur eine "Fake-Konstante", oder?
SYSEX_SYSTEM_INQUIRY:
array [0..5] of Byte = ( $F0, $7E, $00, $06, $01, $F7 ); |
AW: Unterschiede zwischen Konstanten-Deklaration?
jupp
Delphi-Quellcode:
Darum sind/können Pointer-Spielereien auch mal gefährlich werden ... vorallem wenn man ausversehn irgendwelche Schutzmaßnahmen umgeht.
const Test: array [0..3] of Byte = (11, 22, 33, 44);
type PHack = ^THack; THack = array [0..3] of Byte; procedure TForm5.FormCreate(Sender: TObject); begin ShowMessage(IntToStr(Test[1])); PHack(@Test)^[1] := 111; ShowMessage(IntToStr(Test[1])); end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:21 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz