当 Form2 尝试编辑 delphi 中的 form1 时,访问冲突
例如,当我尝试做这样的事情时,
form2.Edit1.text=form1.edit1.text
它会给我一个访问冲突错误。
form2 可以使用 form1 变量的方式是添加
uses Unit1; var Form1:Tform1
到实现
中。程序编译得很好,没有错误,但是当尝试从 form1 实际访问 edit1 时,它会因错误而崩溃。
编辑:
当我将unit1添加到“使用”列表中而不是在实现下而是在接口下时,它似乎正在工作,
这意味着unit2可以编辑unit1,但我现在无法将unit2添加到unit1的接口的使用中 循环单元引用
简而言之,这意味着
- 单元 2 可以编辑/查看单元 1
- 单元 1 无法编辑/查看单元 2
那么有没有一种方法可以使其成为可能,而无需创建由其他两个单元引用的第三帧?
For example when I'm trying to do something like this
form2.Edit1.text=form1.edit1.text
It gives me an access violation error.
The way form2 can use form1 variables is by adding
uses Unit1; var Form1:Tform1
to the implementation
The program compiles fine with no errors but when trying to actually access the edit1 from form1 it crashes with an error.
edit:
It seems like it is working when I'm adding unit1 to the "uses" list not under implementation but under interface
which means unit2 can edit unit1 but I cant add now unit2 to the uses of interface of unit1
circular unit reference
In short it meant
- unit2 can edit/view unit1
- unit1 cant edit/view unit2
So is there a way to make it possible without creating third frame which is refered by the two other units?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的项目中有两个名为
Form1
的变量。 .dpr 文件中的代码创建一个TForm1
并将引用保存在两个Form1
变量中的一个中。然后,您将引用另一个未分配的Form1
。这解释了为什么您会遇到访问冲突。如何修复它?只需从
Unit2
中删除Form1
变量即可。使用“在文件中查找”功能确保整个项目中只有一个Form1
、Form2
等声明。当你正确地做到这一点时,你将毫无困难地让每个单元使用另一个单元,从实现部分,没有循环引用问题。
为了更好地学习和理解这一点,我建议您阅读 .dpr 文件以了解全局变量是如何初始化的。我还建议您阅读 Delphi 语言指南中有关作用域解析的部分,以了解编译器如何解析在同一位置出现两个相同名称的情况。
You have two variables called
Form1
in your project. The code in the .dpr file creates aTForm1
and saves the reference in one of the twoForm1
variables. You then are referring to the other unassignedForm1
.That explains why you are getting the access violation. How to fix it? Simply remove the
Form1
variable fromUnit2
. Use the Find in Files feature to make sure that you have only one declaration ofForm1
,Form2
etc. in your entire project.When you do that correctly you will have no trouble having each unit using the other, from the implementation section, with no circular reference problems.
In order to learn and understand this better I suggest you read the .dpr file to understand how the global variables are initialised. I also recommend that you read the Delphi language guide section on scope resolution to understand how the compiler resolves situations where two identical names are visible at the same location.
无需将 form1 声明为 form2 中的变量 - 将有一个名为“form1”的全局变量。
There is no need to declare form1 as a variable within form2 - there will be a global variable called 'form1'.
将Unit1置于实现下,在Unit2中使用。
不要在 Unit2 中申报 Form1。如果您在 Unit2 中声明 Form1,并且不为其赋值,则它将是一个空指针,在访问时会出现访问冲突。
如果您已释放 Form1 或未创建它,它也会导致访问冲突。
Put Unit1 under implementation, uses in Unit2.
Do not declare Form1 in Unit2. If you declare Form1 in Unit2, and don't assign it a value, it will be a null pointer giving an access violation when acccessed.
If you have freed Form1 or not created it it will also give access violation.
您的项目编译的事实意味着变量“Form2”被声明,因此编译器“已知”。事实上,它给出了访问冲突,这意味着没有有效的对象分配给该变量。有几种可能性。首先,必须通过 IDE(在项目选项中自动创建表单)或通过您的代码创建 Form2。其次,不能有第二个名为“Form2”的变量,它会“隐藏”带有 Form2 对象的变量。
您可以而且必须避免在使用子句中出现循环引用。如果可能的话,在“界面”部分使用它们。当然你可以参考两个以上的单位...
THe fact that your project compiles means, that a variable "Form2" is declared and thus "known" to the compiler. The fact, that it gives a acces violation means, that there is no valid object assigned to this variable. There are a few possibilities. First, Form2 has to be created, by the IDE (auto-create the form in options of the project) or by your code. Second, there must not be a second variable called "Form2" which would "hide" the variable with the Form2-object.
You can and have to aviod having circular references in your uses-clauses. Use them in the "interface"-section if possible. Of course you can reference more than two units...
对我来说正确的答案是将unit1添加到unit2的接口中,并将unit2添加到unit1的实现中
,因为unit1是主窗体,而unit2是由unit1打开的。
因此将unit1添加到unit2的实现中是行不通的。
如果两个单元一起打开的话就可以了。
The right answer for me was Adding unit1 to the interface of unit2 and add unit2 to the implementation of unit1
That is since unit1 is the main form and unit2 is opened by unit1.
so adding unit1 to the implementation of unit2 would not work.
It would've worked if both of the units were opened together.