Get Enumerated type by Inc(type, Index) says "Left Side cannot be assigned to", why?

huangapple go评论61阅读模式
英文:

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(&#39;0&#39;)); // &lt;&lt; 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(&#39;0&#39;));

答案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(&#39;0&#39;)));

huangapple
  • 本文由 发表于 2023年5月18日 11:52:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定