Archived

This topic is now archived and is closed to further replies.

Simple C# linking question

This topic is 4985 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know it''s probably staring me in the face, but I''ve come to C# from a Java background, where the Java compiler automatically finds .java files it needs by searching the current path/classpath. If I have a class in a file called classa.cs, and I want to use that class in another file called main.cs, what do I do? I''m using the .NET command line compiler. Thanks!

Share this post


Link to post
Share on other sites
Well all I do is make sure that both classa.cs and main.cs are in the same namespace and it works. If classa.cs is in a different namespace just delare ''using classanamespace;''

Share this post


Link to post
Share on other sites
Linking is not done via namespaces, it is done via assemblies. The fact that the two usually line up is (mostly) incidental.

Assemblies are usually .dll files. This does not mean they''re win32 DLL files. They aren''t.

Instead assemblies are lumps of IL code, which consist of some classes, and possibly a few other things (like assembly-level attributes).

Anyway, the runtime looks for assemblies in a similar (ish) way that Windows looks for DLLs, except it''s a bit cleverer. It looks initially in the directory of the executable (I think), then in various other standard places (specificially, the GAC, or "Global assembly cache").

The GAC is like a more organised version of the system32 directory for .NET assemblies.

Anyway the way that most app programmers distribute their apps is to ship copies of any other nonstandard assemblies that they need with the exe, and the runtime finds them in the same directory. That''s what I do anyway (And that''s what VS.NET does when you create a dependency of an .exe file project on a library project and build it)

Mark

Share this post


Link to post
Share on other sites
just compile both files together and they are in one assembly.
and of course you can use one class in the other class without linking anything then.

[edited by - cody on April 20, 2004 6:16:42 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by markr
Assemblies are usually .dll files. This does not mean they''re win32 DLL files. They aren''t.


Are too. They may not have any exports, but they are valid PE files.



--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites
in C#, the ASSEMBLY is the closest thing to the normal concept of library (it is ALMOST exactly the same thing) ... and just like dynamic libraries compiled with Visual C++, it creates a dynamically linkable .dll file.

The primary difference are either obvious or largely insignificany. Obvious - the .dll created by C# depends on the .NET runtime, whereas .dll''s creates in C++ might not. Unimportant - the term "assembly" can be used to mean a LOGICAL library, that is actually broken up into more than one .dll file - The only real common use of this is to allow partial download for internet apps - one of the files will be the primary assembly file, and the others will all be dumb dependent files ...

When you want to use code from one .cs file, in another .cs files - there are 2 distinct situations:

1. The two files are part of the same "project" - which really means, they are going to be compiled into the same assembly.

2. The two files are in different projects / assemblies.

In case 1 you do not have to do ANYTHING to use the code - in main.cs you could just write:

classa a = new classa();

and it would work - IF the identifier "classa" can be found without additional information. (more on that in a sec).

In case 2, the assembly that main.cs is in needs a "reference" to the assembly that classa is in. Each visual studio project has a "References" folder which you can right click on and choose "add reference" - then find your assembly with classa.

Back to the namespace isssue. Just like C++, a C# compiler tries to look up identifiers in the current namespace, and in namespaces which have been "imported" using a "using" statement. So if the fully qualified names of your claases were:

MyCompany.SomeApp.main
MyCompany.GameLib.classa

then in main you could write either of the following:

using MyCompany.GameLib;
classa a = new classa();

OR

MyCompany.GameLib.classa a = new MyCompany.GameLib.classa();

...

hope that helps

Share this post


Link to post
Share on other sites
quote:
Original post by davedx
I know it''s probably staring me in the face, but I''ve come to C# from a Java background, where the Java compiler automatically finds .java files it needs by searching the current path/classpath.

If I have a class in a file called classa.cs, and I want to use that class in another file called main.cs, what do I do?



First method: your main class creates one or more objects of type classa, eg.
public classa myclassa = new classa(); 
and then use myclassa.mymethod() or something like that. You can instance it by a method in main or as a variable that gets auto-created like the line above.

Second method: make everything in classa static, and don''t create an object. but call classa.mymethod().

To make it compile, you just specify all .cs files you use on the command line, or in a batch file.

Share this post


Link to post
Share on other sites