deadlock reference in csharp

R

R.Balaji

Hi,

I have declared two interfaces like this

//interface.cs

namespace test
{
interface IInterface1
{
IInterface2 getItem();
}

interface IInterface2
{
IInterface1 getItem();
}
}

I create a dll with this interface.
Now I create two project files.

One implements IInterface1 and the other implements IInterface2


//refer interface.dll
//class1.csproj
//class1.cs

using test;
public class class1 : IInterface1
{
IInterface2 getItem()
{
class2 a = new class2();
return a;
}
}

//refer interface.dll
//class2.csproj
//class2.cs

using test;
public class class2: IInterface2
{
IInterface1 getItem()
{
class1 b = new class1();
return b;
}
}



Now I couldn't compile class1, as it doesn't have reference of class2.
Also I couldn't compile class2, as it doesn't have reference of class1.
What should I do?

Thanks.

Regards,
R.Balaji
 
R

Roman S. Golubin

R.Balaji said:
Hi,

I have declared two interfaces like this
[skip]

Now I couldn't compile class1, as it doesn't have reference of class2.
Also I couldn't compile class2, as it doesn't have reference of class1.

Your have MUST a reference to class to create it or your must load the
assembly that contains this class and use reflection for invoke constructor
of it.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi R.Balaji,
Why don't you reference class1 in class2 project and class2 in class1
project. Otherwise you can't compile the projects because the compiler
doesn't know anything about the other class.
 
R

R.Balaji

Hi,
Thanks for the answer.
But I couldn't compile class1.cs as well class2.cs
Then how would I add reference to the dll?

Regards,
R.Balaji


Roman S. Golubin said:
R.Balaji said:
Hi,

I have declared two interfaces like this
[skip]

Now I couldn't compile class1, as it doesn't have reference of class2.
Also I couldn't compile class2, as it doesn't have reference of class1.

Your have MUST a reference to class to create it or your must load the
assembly that contains this class and use reflection for invoke constructor
of it.
 
R

R.Balaji

Hi,

Thanks for the answer.

I want to refer class2 in class1.
For that I need to compile class2.

But when I compile class2, it requires reference to class1.

I am going into a deadlock situation.

I remember in VC++, we declare just class names and refer the classes.

How do I do it in C#.?

Thanks.

Regards,
R.Balaji

Stoitcho Goutsev (100) said:
Hi R.Balaji,
Why don't you reference class1 in class2 project and class2 in class1
project. Otherwise you can't compile the projects because the compiler
doesn't know anything about the other class.

--
B\rgds
100 [C# MVP]

R.Balaji said:
Hi,

I have declared two interfaces like this

//interface.cs

namespace test
{
interface IInterface1
{
IInterface2 getItem();
}

interface IInterface2
{
IInterface1 getItem();
}
}

I create a dll with this interface.
Now I create two project files.

One implements IInterface1 and the other implements IInterface2


//refer interface.dll
//class1.csproj
//class1.cs

using test;
public class class1 : IInterface1
{
IInterface2 getItem()
{
class2 a = new class2();
return a;
}
}

//refer interface.dll
//class2.csproj
//class2.cs

using test;
public class class2: IInterface2
{
IInterface1 getItem()
{
class1 b = new class1();
return b;
}
}



Now I couldn't compile class1, as it doesn't have reference of class2.
Also I couldn't compile class2, as it doesn't have reference of class1.
What should I do?

Thanks.

Regards,
R.Balaji
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi R.Balaji,

Yes, you are right. You have cyclic dependency. You should avoid that.
I haven't test it, bu try to put class1 and class2 in the same project
(assembly).

--
B\rgds
100 [C# MVP]
R.Balaji said:
Hi,
Thanks for the answer.
But I couldn't compile class1.cs as well class2.cs
Then how would I add reference to the dll?

Regards,
R.Balaji


Roman S. Golubin said:
"R.Balaji" <[email protected]> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
Hi,

I have declared two interfaces like this
[skip]

Now I couldn't compile class1, as it doesn't have reference of class2.
Also I couldn't compile class2, as it doesn't have reference of
class1.

Your have MUST a reference to class to create it or your must load the
assembly that contains this class and use reflection for invoke constructor
of it.
 
R

Roman S. Golubin

Hi, R.Balaji
I want to refer class2 in class1.
For that I need to compile class2.

But when I compile class2, it requires reference to class1.

I am going into a deadlock situation.

I remember in VC++, we declare just class names and refer the classes.

if your use vc++ then your must import this dynamic library with #import
How do I do it in C#.?

u must set refference to class1 :))

or use reflection:

using System.Reflection;
....

Assembly a = Assembly.LoadFrom("library full path\\library full name");
Object o = a.CreateInstance("test.Class1");

return (IInterface1) o;
....
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi R.Balaji,
I remember in VC++, we declare just class names and refer the classes.

Not exactly. You may have the same problem in C++. Forward declaration in
C++ works only if you use pointers or references to the class. Otherwise you
are stuck in the same problem.
How do I do it in C#.?

How I suggested in my previous post try to put both classes in the same
assembly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top