英文:
Get Enumerated type by Inc(type, Index) says "Left Side cannot be assigned to", why?
问题
你的代码部分存在问题。正确的做法是将 Inc
函数的返回值赋给 j
,并确保 TWizPages
是一个枚举类型,而不是整数类型。修正后的代码应该是:
procedure TfrmWizard.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
i, j: TWizPages;
begin
j := Inc(Low(TWizPages), Key-Ord('0')); // 修正此行
end;
请注意,这只是修复了你提供的代码片段中的错误部分。如果你还有其他问题或需要更多帮助,请告诉我。
英文:
I'm more of a C/C++ person, but I'm converting a pascal module from having integer types in a wizard to enumerated types and have the following snag (it is to select a page directly via <kbd>Alt-0</kbd> to <kbd>Alt-9</kbd>):
procedure TfrmWizard.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
i, j: TWizPages;
begin
j := Inc(Low(TWizPages), Key-Ord('0')); // << line with problem
The IDE complains on the comma that the Left side cannot be assigned to
, and at the last parenthesis about Incompatible types
(it doesn't give me much time to read it when I hover over it).
What is the correct way to do what I'm looking to do?
答案1
得分: 3
Inc
是一个过程,不像 Succ
那样是一个函数。它递增第一个不是常数的值。以下是工作版本:
procedure TfrmWizard.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
i, j: TWizPages;
begin
j := Low(TWizPages);
Inc(j, Key-Ord('0'));
英文:
Inc
is a procedure, not a function such as Succ
. It increments the first value which can't be a constant. Here's the working version:
procedure TfrmWizard.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
i, j: TWizPages;
begin
j := Low(TWizPages);
Inc(j, Key-Ord('0'));
答案2
得分: 2
Inc()
需要一个变量的引用,但你传递的是一个常量。
尝试使用以下方式:
j := TWizPages(Ord(Low(TWizPages)) + (Key-Ord('0')));
英文:
Inc()
takes a reference to a variable, but you are passing it a constant.
Try this instead:
j := TWizPages(Ord(Low(TWizPages)) + (Key-Ord('0')));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论