0

when i click on "opens testing2" it goes to form1 of testing2

this is the code for Form1.cs of testing file:

        Testing2.Form1 t2 = new Testing2.Form1();
        t2.Show();
        this.Hide();

When i click on "back to testing" i want Form1.cs of Testing to open.I tried to add reference on Testing2 it gives the following error: enter image description here

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • If it's circular that means you will need to extract it into a separate project/library and have both reference that. – Lloyd Jul 05 '17 at 11:18
  • i have already combined both the projects into Testing – Astral Coder Jul 05 '17 at 11:18
  • Do you mean the `Testing` project or the `Testing` **solution**? – mjwills Jul 05 '17 at 11:20
  • The error is clearly about the *projects* not the forms. You tried to add the `Testing` project as a reference to `Testing2` when `Testing` already has a reference to `Testing. You can't have two projects depend on each other and that's it. Put common types in a common project – Panagiotis Kanavos Jul 05 '17 at 11:20
  • Logically it doesn't make sense that some object A depends on (i.e. references) some other object B, which in turn depends on object A again. This creates a circular dependency that cannot be resolved (object A changes > object B needs rebuilding, but then it changes too > object A needs rebuilding, but then it changes again....). This sounds a bit like some kind of [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is it that you're actually trying to do? – bassfader Jul 05 '17 at 11:21
  • @bassfader the problem is obvious. The error message clearly complains about the *projects* not the forms. This will appear only when *adding* a reference. The OP thought that the message refered to something else – Panagiotis Kanavos Jul 05 '17 at 11:21
  • Testing is the name of the project1 and Testing2 is the name of project2 – Astral Coder Jul 05 '17 at 11:22
  • @Pankaj did you read the message? It has nothing to do with your code. It has to do with the references you added from one project to another – Panagiotis Kanavos Jul 05 '17 at 11:23
  • @PanagiotisKanavos Ahhh "objects" is probably the wrong term. I did not mean `System.Object` objects, but was rather trying to explain the "circular reference" thing in a generic way, so "some object A" is actually meaning one of the projects... I tried avoiding specific terms, but I guess that didn't work out as intended -.- – bassfader Jul 05 '17 at 11:24
  • can anyone just give simple solution to this problem by showing actual working code ? – Astral Coder Jul 05 '17 at 12:44
  • Thanks a lot @mjwills . the code works perfectly for my project. – Astral Coder Jul 06 '17 at 14:20
  • @mjwills This is a circular *project* dependency. Your fiddle doesn't fix this. It doesn't happen because one property was set to something else. It happens only when you click `Add Reference` and try to reference a project that already references the current project – Panagiotis Kanavos Jul 06 '17 at 14:29
  • @mjwills I don't have to. I understand what you did, so the OP *stopped* trying to create a circular reference. The question text though is unclear. Either the question would have to be rewritten or you'd have to explain what's wrong in the answer. The fiddle isn't enough. – Panagiotis Kanavos Jul 06 '17 at 14:33
  • @mjwills it's the *entire* text that is unclear, and the error screenshot. Not just the title. Your code has nothing to do with the circular project reference. – Panagiotis Kanavos Jul 06 '17 at 14:34

1 Answers1

0

In Testing2.Form1, add a property:

public Form CallingForm {get; set;}

Then change:

Testing2.Form1 t2 = new Testing2.Form1();
t2.Show();
this.Hide();

to:

Testing2.Form1 t2 = new Testing2.Form1();
t2.CallingForm = this;
t2.Show();
this.Hide();

Then when you want to show the original form from Testing2.Form1, then do:

if (CallingForm != null)
    CallingForm.Show();

This will work for your purposes - but you will be unable to actually instantiate Testing.Form1 from Testing2.Form1 (since that will indeed introduce a circular dependency).

mjwills
  • 23,389
  • 6
  • 40
  • 63