Thread: Hi! I'd like to understand how to include classes from one file into another.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    13

    Hi! I'd like to understand how to include classes from one file into another.

    How y'all doin'?

    My question wasn't quite strait-forward. I have succesfully used classes of one file in another but i still dont quite understand it. what i've done is "using Class;" in the mainfile, and compiling them both together. So my question is: "Can I compile them both seperately then use them together?"

    My Java is inadequate and foggy, but I think i remember compiling a *.java file to a *.class file and then using a Class from the *.class file in other programs. Can you do this in C#? what are some ways to use different files together?
    THanks guys 'n' gals,
    Tim!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. See this link: C# 4.0 - Lesson 02: Introduction to C# Projects near the bottom of the lesson.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    13
    THANKS! much appreciated,

    and

    sorry to bother again, but all of his examples use visual studio, which i don't have, so im just trying to be completely sure that i got this.
    from what i gathered:
    the .class in java is kindof like .dll in .net no?

    thanks again!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by timmmay View Post
    sorry to bother again, but all of his examples use visual studio, which i don't have, so im just trying to be completely sure that i got this.
    from what i gathered:
    the .class in java is kindof like .dll in .net no?

    thanks again!
    Well it's free to download. Also, the lower half shows how to compile via the command line, which is why I specifically mentioned the bottom part of the example:
    Quote Originally Posted by the example I was talking about
    If you are creating your application using a text editor and if you create many files, when compiling the project, you must remember to reference each file. To do that, in the last section, add the name of each file with its extension:

    csc FileName1.cs FileName2.cs FileName_n.cs

    The executable you get is the one you can use on other computers and that you can distribute to other people.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    13
    sorry. iwasnt clear. i use linux and freebsd, so csc.exe is not available and neither is VS, i actually have a VS cd sitting in the room with me, but have nothing to run it on. i use mono, so maybe my problem is more complex than i originally thought

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have you read something like Mono Basics - Mono yet then?

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    13
    yes. the mono site was one of the first places i looked and ive been returning alot hoping to find something i didnt see. it includes info about using other peoples .dll , but not using gmcs to create them. so it must be that im looking for the wrong information, aka i dont know what to do so i cant find how to do it. but thanks. i guess i just need to keep looking.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    13
    I GOT IT!!! wow...
    I couldn't get it with command line, but using MonoDevelop IDE i finally found my way to the library creator, then mono hid the .dll file in a really obscure place (./bin/DEBUG/something) then compile the file that uses the dll with "gmcs mainfile.cs -r:assembly.dll" (this part actually was in the mono docs). thank you all very much, its been a trying day, but ive learned something and thats what really counts.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by timmmay View Post
    sorry to bother again, but all of his examples use visual studio, which i don't have, so im just trying to be completely sure that i got this.
    from what i gathered:
    the .class in java is kindof like .dll in .net no?
    Not exactly since a DLL file in C# can hold more than one class. It resembles a .class file in many other ways, though. It is however created explicitly through compiler switches and from an independent project.

    My Java is inadequate and foggy, but I think i remember compiling a *.java file to a *.class file and then using a Class from the *.class file in other programs. Can you do this in C#?
    Yes. You create your C# DLL project and code in all the necessary classes you wish to be a part of the DLL. Traditionally, inside that project we use a source file for each class, named -- at least partially -- after the class name. All classes are made to belong to a namespace (usually this is a common namespace for all the classes inside the DLL, but not always) so the classes are properly confined when being imported into other projects. We then compile and generate the DLL.

    To import that DLL into another project, we add the compiled DLL as a reference to that new project. Where we make calls to objects and methods inside the DLL we add a using directive to any relevant namespaces we created inside the DLL.

    what are some ways to use different files together?
    This is a broad topic. But simplifying it:

    Source files:
    - You can put your entire project inside the same source file
    - You can put each class, each interfaces, each enumerators and other entities into separate source files
    - You can combine the two above to create source files that have more than one class, or some classes and an interface, etc...
    - You can separate a class into several source files by using the partial keyword

    Projects:
    - You can compile your project into a single executable or dll
    - You can create a solution and develop several independent projects (usually one executable and several DLLs) belonging to that solution (this is the common approach to software development in C# for any project other than the simplest ones). I don't know if your IDE supports this, but you should look for one that does.

    Solutions:
    - For large, complex systems you can create several solutions in either a partitioned solution or multi-solution model (see Structuring Solutions and Projects). See also Rules to Better Large Builds for an alternative approach (dubbed Mick's Recommended Approach, point 4.3)
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fatal error C1083: Cannot open include file-Using Classes
    By ibleedart in forum C++ Programming
    Replies: 3
    Last Post: 07-12-2007, 04:38 AM
  2. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 01:45 PM
  3. Redefinition of classes (#include headers)
    By cjschw in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2003, 09:24 PM
  4. why wont MS Visual C++ understand my #include <stdlib.h>
    By Master_Rondal in forum C Programming
    Replies: 5
    Last Post: 10-15-2002, 04:36 PM
  5. Can't include Classes(very strange)
    By retretret in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2002, 12:41 PM

Tags for this Thread