Bug in C# compiler

G

Guest

Hi,

I think there is a bug in VS 2003 as well as VS 2005 C# compiler. Let me
explain:

I have created 3 projects in my solution:

ClassLibrary1, ClassLibrary2 and ClassLibrary3.

Now each of these libraries has a single public class in it (classes r named
as A, B and C respectively) and the class defined in 2nd library is derived

from the class in first library as:
************
//in ClassLibrary1
public class A
{
//constructor
}



//in ClassLibrary2

using ClassLibrary1;

public class B : A
{
//public constructor
}


//in ClassLibrary2

using ClassLibrary2;

public class C : A
{
//public constructor
public C()
{
B b1 = new B();
}
}

Here, ClassLibrary1 is referenced in project ClassLibrary2 and ClassLibrary2
is referenced in project ClassLibrary3.

Also, Class C defined in ClassLibrary3 is using an object of B in it.

But when i compile this code, i get an error saying:

e:\ClassLibrary3\bin\Debug\ClassLibrary2.dll Referenced class
'ClassLibrary2.B' has base class or interface 'ClassLibrary1.A' defined in an
assembly that is

not referenced. You must add a reference to assembly 'ClassLibrary1'.

According to me, we dont need to reference ClassLibrary1 as it is already
being referenced by ClassLibrary2 so this should compile fine. But i may be
wrong.

I owuld be grateful if someone could shed light on this.

Thanks

Vivek
 
J

Joanna Carter [TeamB]

"Vivek Thakur" <vivekatvivekthakur(donotspam)com> a écrit dans le message de
news: (e-mail address removed)...

| But when i compile this code, i get an error saying:
|
| e:\ClassLibrary3\bin\Debug\ClassLibrary2.dll Referenced class
| 'ClassLibrary2.B' has base class or interface 'ClassLibrary1.A' defined in
an
| assembly that is
|
| not referenced. You must add a reference to assembly 'ClassLibrary1'.
|
| According to me, we dont need to reference ClassLibrary1 as it is already
| being referenced by ClassLibrary2 so this should compile fine. But i may
be
| wrong.

According to the compiler, you are wrong and it is right. There is no bug in
the compiler, references are not inferred, they have to be explicit.

Joanna
 
G

Guest

Thanks Joanna,

But i have a project where this is working. If i do the same thing in visual
studio 2005 and instead of class library3 i create a WebService project and
add ClassLibrary2's reference, then it works.

Please let me know where am i wrong in my example. Why do i need to
reference 1st project when i am not using it at all? And why does the same
thing works in VS 2005 if the third project is a WebService?
 
B

Barry Kelly

Vivek Thakur said:
But i have a project where this is working. If i do the same thing in visual
studio 2005 and instead of class library3 i create a WebService project and
add ClassLibrary2's reference, then it works.

Please let me know where am i wrong in my example. Why do i need to
reference 1st project when i am not using it at all? And why does the same
thing works in VS 2005 if the third project is a WebService?

Webservices use a different model for referencing DLLs, since they are
ASP.NET pages. All assemblies referenced by your WebService get copied
to the Bin directory in the website. There is no project file with a
"references" list like there is for class libraries. Instead, the
ASP.NET compiler will reference all the assemblies in the Bin directory
automatically when it's compiling your webservice.

-- Barry
 
G

Guest

Thanks Berry,

But do Web projects also use the same model? even if the third project is a
website, i see the same results (as when i create a webservice).
 
G

Guest

Also, one more thing as far as copying the DLLs is concerned:

In VS 2003, when i compile ClassLibrary3 project, i can see the assemblies
from ClassLibrary2 and ClassLibrary1 (too) even when i have not referenced
ClassLibrary1. So assemblies are copied in the bin folder of class library 3
but since i get an error the project does not compile.

This is confusing me. Let me know yr thgts on this.

thanks again
 
B

Barry Kelly

Vivek Thakur said:
Thanks Berry,

But do Web projects also use the same model? even if the third project is a
website, i see the same results (as when i create a webservice).

ASP.NET applications follow this model.

-- Barry
 
B

Barry Kelly

Vivek Thakur said:
Also, one more thing as far as copying the DLLs is concerned:

In VS 2003, when i compile ClassLibrary3 project, i can see the assemblies
from ClassLibrary2 and ClassLibrary1 (too) even when i have not referenced
ClassLibrary1. So assemblies are copied in the bin folder of class library 3
but since i get an error the project does not compile.

IIRC with VS 2003, there was a project file for ASP.NET applications.
I don't recall all the details, it is some years since I used VS 2003.

-- Barry
 
G

Guest

So basically even when we have DLLs in the /bin directory, the project wont
compile. i still do not understand this.
 
A

Amry

If I am not mistaken, for web projects, every DLL contained in its bin
directory will be automatically referred and loaded, whereas for other
types of projects, you must explicitly refer to the DLL you want in
order for it to compile.
 
M

Markus Ewald

Vivek said:
Also, one more thing as far as copying the DLLs is concerned:

In VS 2003, when i compile ClassLibrary3 project, i can see the assemblies
from ClassLibrary2 and ClassLibrary1 (too) even when i have not referenced
ClassLibrary1. So assemblies are copied in the bin folder of class library 3
but since i get an error the project does not compile.

This is confusing me. Let me know yr thgts on this.

thanks again

The files are always being copied because they are required to run the
application. For example, if A uses B and B uses C, then all three
assemblies will end up int A's bin folder.

References don't really have anything to do with files. If B does not
use any of the types of C in its public interface (by deriving or
expecting them as parameter), then B could be referenced needing another
reference to C (even if it is ultimately required in the bin folder)

However, if B makes use of the types of C in its public interface, then
any assembly that wants to use those methods or classes that are making
use of C's public types will need to also reference C in addition to B.

-Markus-
 

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