0

I'm trying convert a string to VKCode using this reference, but when try compile, comes the following error's:

E2001 Ordinal type required

E2010 Incompatible types: 'Integer' and 'string'

This means that case (expression) of not is a option to be used here?

function VKStringToWord(const aVKstring: string): Word;
begin
  if aVKstring = '' then
    Exit(0);
  case UpperCase(aVKstring) of
    'VK_A':
      Result := $41;
  else
    Result := 0;
  end;
end;

1 Answer 1

2

Correct, you cannot use case with strings. According to the documentation:

selectorExpression is any expression of an ordinal type

(emphasis mine). Strings are not ordinal types.

What you can do, however, is something like this:

function VKStringToWord(const AVKString: string): Word;
const
  Data: array[0..192] of record
    Key: string;
    Val: Word
  end =
    (
      (Key: 'VK_0'                   ; Val: $30),
      (Key: 'VK_1'                   ; Val: $31),
      (Key: 'VK_2'                   ; Val: $32),
      (Key: 'VK_3'                   ; Val: $33),
      (Key: 'VK_4'                   ; Val: $34),
      (Key: 'VK_5'                   ; Val: $35),
      (Key: 'VK_6'                   ; Val: $36),
      (Key: 'VK_7'                   ; Val: $37),
      (Key: 'VK_8'                   ; Val: $38),
      (Key: 'VK_9'                   ; Val: $39),
      (Key: 'VK_A'                   ; Val: $41),
      (Key: 'VK_ACCEPT'              ; Val: 30),
      (Key: 'VK_ADD'                 ; Val: 107),
      (Key: 'VK_APPS'                ; Val: $5D),
      (Key: 'VK_ATTN'                ; Val: $F6),
      (Key: 'VK_B'                   ; Val: $42),
      (Key: 'VK_BACK'                ; Val: 8),
      (Key: 'VK_BROWSER_BACK'        ; Val: $A6),
      (Key: 'VK_BROWSER_FAVORITES'   ; Val: $AB),
      (Key: 'VK_BROWSER_FORWARD'     ; Val: $A7),
      (Key: 'VK_BROWSER_HOME'        ; Val: $AC),
      (Key: 'VK_BROWSER_REFRESH'     ; Val: $A8),
      (Key: 'VK_BROWSER_SEARCH'      ; Val: $AA),
      (Key: 'VK_BROWSER_STOP'        ; Val: $A9),
      (Key: 'VK_C'                   ; Val: $43),
      (Key: 'VK_CANCEL'              ; Val: 3),
      (Key: 'VK_CAPITAL'             ; Val: 20),
      (Key: 'VK_CLEAR'               ; Val: 12),
      (Key: 'VK_CONTROL'             ; Val: 17),
      (Key: 'VK_CONVERT'             ; Val: 28),
      (Key: 'VK_CRSEL'               ; Val: $F7),
      (Key: 'VK_D'                   ; Val: $44),
      (Key: 'VK_DECIMAL'             ; Val: 110),
      (Key: 'VK_DELETE'              ; Val: 46),
      (Key: 'VK_DIVIDE'              ; Val: 111),
      (Key: 'VK_DOWN'                ; Val: 40),
      (Key: 'VK_E'                   ; Val: $45),
      (Key: 'VK_END'                 ; Val: 35),
      (Key: 'VK_EREOF'               ; Val: $F9),
      (Key: 'VK_ESCAPE'              ; Val: 27),
      (Key: 'VK_EXECUTE'             ; Val: 43),
      (Key: 'VK_EXSEL'               ; Val: $F8),
      (Key: 'VK_F'                   ; Val: $46),
      (Key: 'VK_F1'                  ; Val: 112),
      (Key: 'VK_F10'                 ; Val: 121),
      (Key: 'VK_F11'                 ; Val: 122),
      (Key: 'VK_F12'                 ; Val: 123),
      (Key: 'VK_F13'                 ; Val: 124),
      (Key: 'VK_F14'                 ; Val: 125),
      (Key: 'VK_F15'                 ; Val: 126),
      (Key: 'VK_F16'                 ; Val: 127),
      (Key: 'VK_F17'                 ; Val: 128),
      (Key: 'VK_F18'                 ; Val: 129),
      (Key: 'VK_F19'                 ; Val: 130),
      (Key: 'VK_F2'                  ; Val: 113),
      (Key: 'VK_F20'                 ; Val: 131),
      (Key: 'VK_F21'                 ; Val: 132),
      (Key: 'VK_F22'                 ; Val: 133),
      (Key: 'VK_F23'                 ; Val: 134),
      (Key: 'VK_F24'                 ; Val: 135),
      (Key: 'VK_F3'                  ; Val: 114),
      (Key: 'VK_F4'                  ; Val: 115),
      (Key: 'VK_F5'                  ; Val: 116),
      (Key: 'VK_F6'                  ; Val: 117),
      (Key: 'VK_F7'                  ; Val: 118),
      (Key: 'VK_F8'                  ; Val: 119),
      (Key: 'VK_F9'                  ; Val: 120),
      (Key: 'VK_FINAL'               ; Val: 24),
      (Key: 'VK_G'                   ; Val: $47),
      (Key: 'VK_H'                   ; Val: $48),
      (Key: 'VK_HANGUL'              ; Val: 21),
      (Key: 'VK_HANJA'               ; Val: 25),
      (Key: 'VK_HELP'                ; Val: 47),
      (Key: 'VK_HIGHESTVALUE'        ; Val: $FFFF),
      (Key: 'VK_HOME'                ; Val: 36),
      (Key: 'VK_I'                   ; Val: $49),
      (Key: 'VK_INSERT'              ; Val: 45),
      (Key: 'VK_J'                   ; Val: $4A),
      (Key: 'VK_JUNJA'               ; Val: 23),
      (Key: 'VK_K'                   ; Val: $4B),
      (Key: 'VK_KANA'                ; Val: 21),
      (Key: 'VK_KANJI'               ; Val: 25),
      (Key: 'VK_L'                   ; Val: $4C),
      (Key: 'VK_LAUNCH_APP1'         ; Val: $B6),
      (Key: 'VK_LAUNCH_APP2'         ; Val: $B7),
      (Key: 'VK_LAUNCH_MAIL'         ; Val: $B4),
      (Key: 'VK_LAUNCH_MEDIA_SELECT' ; Val: $B5),
      (Key: 'VK_LBUTTON'             ; Val: 1),
      (Key: 'VK_LCL_ALT'             ; Val: 18),
      (Key: 'VK_LCL_AT'              ; Val: $103),
      (Key: 'VK_LCL_BACKSLASH'       ; Val: $DC),
      (Key: 'VK_LCL_CALL'            ; Val: $101),
      (Key: 'VK_LCL_CAPSLOCK'        ; Val: 20),
      (Key: 'VK_LCL_CLOSE_BRAKET'    ; Val: $DD),
      (Key: 'VK_LCL_COMMA'           ; Val: $BC),
      (Key: 'VK_LCL_ENDCALL'         ; Val: $102),
      (Key: 'VK_LCL_EQUAL'           ; Val: $BB),
      (Key: 'VK_LCL_LALT'            ; Val: $A4),
      (Key: 'VK_LCL_MINUS'           ; Val: $BD),
      (Key: 'VK_LCL_OPEN_BRAKET'     ; Val: $DB),
      (Key: 'VK_LCL_POINT'           ; Val: $BE),
      (Key: 'VK_LCL_POWER'           ; Val: $100),
      (Key: 'VK_LCL_QUOTE'           ; Val: $DE),
      (Key: 'VK_LCL_RALT'            ; Val: $A5),
      (Key: 'VK_LCL_SEMI_COMMA'      ; Val: $BA),
      (Key: 'VK_LCL_SLASH'           ; Val: $BF),
      (Key: 'VK_LCL_TILDE'           ; Val: $C0),
      (Key: 'VK_LCONTROL'            ; Val: $A2),
      (Key: 'VK_LEFT'                ; Val: 37),
      (Key: 'VK_LMENU'               ; Val: $A4),
      (Key: 'VK_LSHIFT'              ; Val: $A0),
      (Key: 'VK_LWIN'                ; Val: $5B),
      (Key: 'VK_M'                   ; Val: $4D),
      (Key: 'VK_MBUTTON'             ; Val: 4),
      (Key: 'VK_MEDIA_NEXT_TRACK'    ; Val: $B0),
      (Key: 'VK_MEDIA_PLAY_PAUSE'    ; Val: $B3),
      (Key: 'VK_MEDIA_PREV_TRACK'    ; Val: $B1),
      (Key: 'VK_MEDIA_STOP'          ; Val: $B2),
      (Key: 'VK_MENU'                ; Val: 18),
      (Key: 'VK_MODECHANGE'          ; Val: 31),
      (Key: 'VK_MULTIPLY'            ; Val: 106),
      (Key: 'VK_N'                   ; Val: $4E),
      (Key: 'VK_NEXT'                ; Val: 34),
      (Key: 'VK_NONAME'              ; Val: $FC),
      (Key: 'VK_NONCONVERT'          ; Val: 29),
      (Key: 'VK_NUMLOCK'             ; Val: $90),
      (Key: 'VK_NUMPAD0'             ; Val: 96),
      (Key: 'VK_NUMPAD1'             ; Val: 97),
      (Key: 'VK_NUMPAD2'             ; Val: 98),
      (Key: 'VK_NUMPAD3'             ; Val: 99),
      (Key: 'VK_NUMPAD4'             ; Val: 100),
      (Key: 'VK_NUMPAD5'             ; Val: 101),
      (Key: 'VK_NUMPAD6'             ; Val: 102),
      (Key: 'VK_NUMPAD7'             ; Val: 103),
      (Key: 'VK_NUMPAD8'             ; Val: 104),
      (Key: 'VK_NUMPAD9'             ; Val: 105),
      (Key: 'VK_O'                   ; Val: $4F),
      (Key: 'VK_OEM_1'               ; Val: $BA),
      (Key: 'VK_OEM_102'             ; Val: $E2),
      (Key: 'VK_OEM_2'               ; Val: $BF),
      (Key: 'VK_OEM_3'               ; Val: $C0),
      (Key: 'VK_OEM_4'               ; Val: $DB),
      (Key: 'VK_OEM_5'               ; Val: $DC),
      (Key: 'VK_OEM_6'               ; Val: $DD),
      (Key: 'VK_OEM_7'               ; Val: $DE),
      (Key: 'VK_OEM_8'               ; Val: $DF),
      (Key: 'VK_OEM_CLEAR'           ; Val: $FE),
      (Key: 'VK_OEM_COMMA'           ; Val: $BC),
      (Key: 'VK_OEM_MINUS'           ; Val: $BD),
      (Key: 'VK_OEM_PERIOD'          ; Val: $BE),
      (Key: 'VK_OEM_PLUS'            ; Val: $BB),
      (Key: 'VK_P'                   ; Val: $50),
      (Key: 'VK_PA1'                 ; Val: $FD),
      (Key: 'VK_PAUSE'               ; Val: 19),
      (Key: 'VK_PLAY'                ; Val: $FA),
      (Key: 'VK_PRINT'               ; Val: 42),
      (Key: 'VK_PRIOR'               ; Val: 33),
      (Key: 'VK_PROCESSKEY'          ; Val: $E7),
      (Key: 'VK_Q'                   ; Val: $51),
      (Key: 'VK_R'                   ; Val: $52),
      (Key: 'VK_RBUTTON'             ; Val: 2),
      (Key: 'VK_RCONTROL'            ; Val: $A3),
      (Key: 'VK_RETURN'              ; Val: 13),
      (Key: 'VK_RIGHT'               ; Val: 39),
      (Key: 'VK_RMENU'               ; Val: $A5),
      (Key: 'VK_RSHIFT'              ; Val: $A1),
      (Key: 'VK_RWIN'                ; Val: $5C),
      (Key: 'VK_S'                   ; Val: $53),
      (Key: 'VK_SCROLL'              ; Val: $91),
      (Key: 'VK_SELECT'              ; Val: 41),
      (Key: 'VK_SEPARATOR'           ; Val: 108),
      (Key: 'VK_SHIFT'               ; Val: 16),
      (Key: 'VK_SLEEP'               ; Val: $5F),
      (Key: 'VK_SNAPSHOT'            ; Val: 44),
      (Key: 'VK_SPACE'               ; Val: 32),
      (Key: 'VK_SUBTRACT'            ; Val: 109),
      (Key: 'VK_T'                   ; Val: $54),
      (Key: 'VK_TAB'                 ; Val: 9),
      (Key: 'VK_U'                   ; Val: $55),
      (Key: 'VK_UNDEFINED'           ; Val: $FF),
      (Key: 'VK_UNKNOWN'             ; Val: 0),
      (Key: 'VK_UP'                  ; Val: 38),
      (Key: 'VK_V'                   ; Val: $56),
      (Key: 'VK_VOLUME_DOWN'         ; Val: $AE),
      (Key: 'VK_VOLUME_MUTE'         ; Val: $AD),
      (Key: 'VK_VOLUME_UP'           ; Val: $AF),
      (Key: 'VK_W'                   ; Val: $57),
      (Key: 'VK_X'                   ; Val: $58),
      (Key: 'VK_XBUTTON1'            ; Val: 5),
      (Key: 'VK_XBUTTON2'            ; Val: 6),
      (Key: 'VK_Y'                   ; Val: $59),
      (Key: 'VK_Z'                   ; Val: $5A),
      (Key: 'VK_ZOOM'                ; Val: $FB)
    );
begin

  for var VK in Data do
    if SameText(VK.Key, AVKString) then
      Exit(VK.Val);

  Result := 0;

end;
Sign up to request clarification or add additional context in comments.

1 Comment

Fwiw, while I was doing commercial Delphi development, I never really felt any burning need for any of the post-D7 language changes apart from Unicode support, especially not generics or enhanced RTTI, etc. Otoh, string-case support would give the biggest single increase in practical expressive power and concision the language could have. Imo, ymmv, etc ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.