简体   繁体   English

Delphi 书签错误:E2003 未声明的标识符“TBookmark”

[英]Delphi Bookmark Error: E2003 Undeclared identifier 'TBookmark'

Hey I wanted to use a TBookmark as a varialbe in my Form.嘿,我想在我的表单中使用 TBookmark 作为变量。 I got it running in another Form and it is working there.我让它以另一种形式运行,它在那里工作。

But in the new Form I get the Error.. I guess I have to include something in the uses statement but I cant remember what it was.但是在新表单中我得到了错误。我想我必须在使用语句中包含一些东西,但我不记得它是什么。 Here is the code TBookmark is underlined in red so thats where the error sits.这是代码 TBookmark 带有红色下划线,因此这就是错误所在。

procedure TForm4.FormCreate(Sender: TObject);
var test : string;
var selectedRow, rows : TBookmark;
begin
  rows := Form1.DBGrid1.DataSource.DataSet.GetBookmark;
  Form1.DBGrid1.SelectedRows.CurrentRowSelected := True;
  Form1.DBGrid1.DataSource.DataSet.GotoBookmark(rows);
  test := Form1.DBGrid1.DataSource.DataSet.FieldByName('name').AsString;
  ShowMessage(test);

end;

end.

Your Form4 needs to Use the DB unit, because that's where TBookMark is declared.您的 Form4 需要使用 DB 单元,因为这是声明 TBookMark 的地方。

Btw, what is in Form1's unit is irrelevant to this.顺便说一句,Form1 单元中的内容与此无关。 The only relevant thing is that Form4's unit has to Use DB.唯一相关的是Form4的单元必须使用DB。 What happens is that when the compiler tries to compile your Form4 unit, it needs to be able to find the definition of TBookMark, and that is in the standard DB.Pas unit along with lots of other dataset-related stuff.发生的情况是,当编译器尝试编译您的 Form4 单元时,它需要能够找到 TBookMark 的定义,即标准 DB.Pas 单元以及许多其他与数据集相关的内容。 The same is true of any other identifier (or its class) that the compiler encounters in your project's source code.编译器在项目源代码中遇到的任何其他标识符(或其类)也是如此。

99% of problems like this can be solved by doing a "Search | Find in Files" through Dephi's source code folders (and your project's folder if it's one of yours) to identify where the "undeclared" or missing item is declared. 99% 的此类问题可以通过 Dephi 的源代码文件夹(以及您的项目文件夹,如果它是您的文件夹)执行“搜索|在文件中查找”来确定声明“未声明”或缺失项目的位置来解决。

Update So, you've got this code, which I'll assume is in your uForm4.Pas unit.更新所以,你有这个代码,我假设它在你的 uForm4.Pas 单元中。

  procedure TForm4.FormCreate(Sender: TObject);
  var
    test : string;
  var
    selectedRow, rows : TBookmark;
  begin
    rows := Form1.DBGrid1.DataSource.DataSet.GetBookmark;
    Form1.DBGrid1.SelectedRows.CurrentRowSelected := True;
    Form1.DBGrid1.DataSource.DataSet.GotoBookmark(rows);
    test := Form1.DBGrid1.DataSource.DataSet.FieldByName('name').AsString;
    ShowMessage(test);
  end;

You want to be able to do something with the Name value that's shown in the current row of DBGrid1 on Form1.您希望能够对 Form1 上 DBGrid1 的当前行中显示的 Name 值执行某些操作。 There's nothing particularly wrong with the way you've done it, just that it's long-winded, error-prone and invites problems like the one you've having with TBookMark.你做这件事的方式没有什么特别的问题,只是它冗长,容易出错,并且会引起你在使用 TBookMark 时遇到的问题。

The point is that somewhere in your project, maybe in your uForm1.Pas unit, you know, I don't, there must be a TDataSet-descendant (like TFDQuery, TAdoQuery or TTable) that is specified in the DataSet property of Form1's DataSource1.关键是在您的项目中的某个地方,可能在您的 uForm1.Pas 单元中,您知道,我没有,必须在 Form1 的 DataSource1 的DataSet属性中指定一个 TDataSet-descendant(如 TFDQuery、TAdoQuery 或 TTable) . For the sake of argument, lets' say that the dataset component is FDQuery1 on Form1 and you want to get the Name field value from the current row in DBGrid1.为了论证,假设数据集组件是 Form1 上的 FDQuery1,并且您希望从 DBGrid1 中的当前行获取 Name 字段值。

To get that Name value, you don't actually need the bookmarks your code is using.要获得该 Name 值,您实际上并不需要您的代码正在使用的书签。 The way a TDBGrid works, the currently-selected row in the grid is always the current row in the dataset component. TDBGrid 的工作方式是,网格中当前选定的行始终是数据集组件中的当前行。 So you could simply write所以你可以简单地写

    procedure TForm4.FormCreate(Sender: TObject);
    var
      test : string;
    begin
      test := Form1.FDQuery1.FieldByName('name').AsString;
      ShowMessage(test);
    end;

because you don't need to go through the rigmarole of Form1.DBGrid1.DataSource.DataSet to get to it.因为您不需要通过 Form1.DBGrid1.DataSource.DataSet 的Form1.DBGrid1.DataSource.DataSet来获取它。

Now, to explain another little mystery, how come your code would work fine if it was in uForm1.Pas but you get the Undeclared Identifier: TBookMark error why you try the same code in uForm4.Pas unit?现在,为了解释另一个小谜团,为什么你的代码在 uForm1.Pas 中可以正常工作,但你得到Undeclared Identifier: TBookMark错误为什么你在 uForm4.Pas 单元中尝试相同的代码? Well, if you've ever watched the top of a source code file as it's being saved, you'll notice that Delphi automatically adds, to the Uses list at the top, the units which contain any of the components you've added to the form since its last save.好吧,如果您曾经看过源代码文件的顶部保存,您会注意到 Delphi 会自动将包含您添加到的任何组件的单元添加到顶部的使用列表自上次保存后的表单。 So adding a TDataSource to the form would add the DB unit to the Uses list, because that's where TDataSource is declared and so is TBookMark.因此,将 TDataSource 添加到表单会将 DB 单元添加到 Uses 列表,因为这是声明 TDataSource 的地方,TBookMark 也是如此。 Which is why Delphi could compile Form1's code without the error, whereas when you try to mention a TBookMark to uForm4, you need to add it to the unit's Uses list unless you add a component (like TDataSource) to Form4 which will cause it to automatically add DB to the Uses list if it isn't already there.这就是为什么 Delphi 可以编译 Form1 的代码而不会出现错误的原因,而当您尝试向 uForm4 提及 TBookMark 时,您需要将其添加到单元的使用列表中,除非您将组件(如 TDataSource)添加到 Form4,这将导致它自动如果 DB 尚不存在,请将其添加到 Uses 列表中。 Mystery solved.谜团已揭开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM