2
votes

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?

5
It seems like It is working when im 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 In short it meant - unit2 can edit/view unit1 - unit1 cant edit/view unit2 So is there a way to make it possible?Evyatar
As No'am Newman pointed out, make sure you are not redeclaring the Form1 variable. Also, check in Project->Options->Forms if you have both Form1 and Form2 in Auto-create forms list.Wodzu

5 Answers

3
votes

You have two variables called Form1 in your project. The code in the .dpr file creates a TForm1 and saves the reference in one of the two Form1 variables. You then are referring to the other unassigned Form1.

That explains why you are getting the access violation. How to fix it? Simply remove the Form1 variable from Unit2. Use the Find in Files feature to make sure that you have only one declaration of Form1, 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.

2
votes

There is no need to declare form1 as a variable within form2 - there will be a global variable called 'form1'.

0
votes

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.

0
votes

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...

0
votes

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.