locked
Using Classes From My Other Projects RRS feed

  • Question

  • For Context: I've got three separate classes in separate projects that are supposed to accomplish a similar task for three separate file types. (I doubt this matters, but they're for inserting externally loaded images into office documents using open xml). 

    My question: How can I use all three classes in a separate class? I want to create a new, fourth class that can use the functionality of the previous three, but how do I access them? I tried "using" statements, but that did not appear to work. I'm aware that the easiest thing to do would be to just copy and paste the code from the other classes into the new one, but that would be a lot of lines in one file and I'd like to know if there's a way around that for future reference.

    Thursday, August 27, 2015 7:50 PM

Answers

  • Go to the project containing your fourth class. Right click on the project references Clikck Add Reference and add other three projects that contain three classes Now when you type class name and Ctrl + . (Dot) and visual studio will suggest you add the namespace and you can access classes now.
    • Proposed as answer by JayChase Friday, August 28, 2015 1:36 PM
    • Marked as answer by Kristin Xie Monday, September 7, 2015 9:25 AM
    Thursday, August 27, 2015 8:10 PM
  • The classes would have to be in a classlib project that your main project that uses them can set reference too and use the classes/objects, which is called Layered Architectural Style. It can be 2  layers or more.

    https://msdn.microsoft.com/en-us/library/ee658117.aspx?f=255&MSPPError=-2147217396

    https://en.wikipedia.org/wiki/Separation_of_concerns

    This is a classic example it is in VB, but you should be able to follow the concept, which in your  case may only be two layers that you need.

    http://www.codeproject.com/Articles/21115/Building-an-N-Tier-Application-in-VB-NET-in-Step

    • Marked as answer by Kristin Xie Monday, September 7, 2015 9:25 AM
    Thursday, August 27, 2015 8:11 PM
  • It would be much more adviseable to make one class that has all 3 classes functionalities, just with a Enumeration to switch between the filetypes (either during open or during constructor call). All that should be different between the 3 cases should be the open/constructor call. For the dumb consumer they should all 3 work the same way.

    You can make them appear to be one class by making a 4th class that encapsualtes them. But these classes are handling file handles and you run the risk of not properly disposing them when switching modes.

    Another pattern worth a look might be the stream class. They have one abstract class. With dozens of implementations. But every code just accepts a "stream" instance, without further bothering what specific stream it is.

    That being said, both .NET .exe and .dll files have the same assembly structure. Both kinds can be added to the project references. Both types will allow other .NET code full access to anything public in them. They might even share the same COM interoperability.
    The only real difference between a .NET exe and dll is that one has a class with a main function and the order to start the .NET Runtime at the start.

    • Edited by Christopher84 Thursday, August 27, 2015 8:22 PM
    • Marked as answer by Kristin Xie Monday, September 7, 2015 9:25 AM
    Thursday, August 27, 2015 8:18 PM

All replies

  • Go to the project containing your fourth class. Right click on the project references Clikck Add Reference and add other three projects that contain three classes Now when you type class name and Ctrl + . (Dot) and visual studio will suggest you add the namespace and you can access classes now.
    • Proposed as answer by JayChase Friday, August 28, 2015 1:36 PM
    • Marked as answer by Kristin Xie Monday, September 7, 2015 9:25 AM
    Thursday, August 27, 2015 8:10 PM
  • The classes would have to be in a classlib project that your main project that uses them can set reference too and use the classes/objects, which is called Layered Architectural Style. It can be 2  layers or more.

    https://msdn.microsoft.com/en-us/library/ee658117.aspx?f=255&MSPPError=-2147217396

    https://en.wikipedia.org/wiki/Separation_of_concerns

    This is a classic example it is in VB, but you should be able to follow the concept, which in your  case may only be two layers that you need.

    http://www.codeproject.com/Articles/21115/Building-an-N-Tier-Application-in-VB-NET-in-Step

    • Marked as answer by Kristin Xie Monday, September 7, 2015 9:25 AM
    Thursday, August 27, 2015 8:11 PM
  • It would be much more adviseable to make one class that has all 3 classes functionalities, just with a Enumeration to switch between the filetypes (either during open or during constructor call). All that should be different between the 3 cases should be the open/constructor call. For the dumb consumer they should all 3 work the same way.

    You can make them appear to be one class by making a 4th class that encapsualtes them. But these classes are handling file handles and you run the risk of not properly disposing them when switching modes.

    Another pattern worth a look might be the stream class. They have one abstract class. With dozens of implementations. But every code just accepts a "stream" instance, without further bothering what specific stream it is.

    That being said, both .NET .exe and .dll files have the same assembly structure. Both kinds can be added to the project references. Both types will allow other .NET code full access to anything public in them. They might even share the same COM interoperability.
    The only real difference between a .NET exe and dll is that one has a class with a main function and the order to start the .NET Runtime at the start.

    • Edited by Christopher84 Thursday, August 27, 2015 8:22 PM
    • Marked as answer by Kristin Xie Monday, September 7, 2015 9:25 AM
    Thursday, August 27, 2015 8:18 PM
  • The only real difference between a .NET exe and dll is that one has a class with a main function and the order to start the .NET Runtime at the start.

    The real difference is that an exe can run standalone,  and a DLL has to be hosted.

    Thursday, August 27, 2015 8:33 PM
  • The only real difference between a .NET exe and dll is that one has a class with a main function and the order to start the .NET Runtime at the start.

    The real difference is that an exe can run standalone,  and a DLL has to be hosted.

    Put a main function into class named after the dll. Compile.
    Instantiate the class, call main on the isntance.

    What did you just do? The exact operation of executing a .NET .exe file!

    Wheter you load the assembly and start from it's own main or load the assembly and start from another assemblies main - what changes?

    Friday, August 28, 2015 10:26 AM
  • The only real difference between a .NET exe and dll is that one has a class with a main function and the order to start the .NET Runtime at the start.

    The real difference is that an exe can run standalone,  and a DLL has to be hosted.

    Put a main function into class named after the dll. Compile.
    Instantiate the class, call main on the isntance.

    What did you just do? The exact operation of executing a .NET .exe file!

    Wheter you load the assembly and start from it's own main or load the assembly and start from another assemblies main - what changes?

    No one goes around doing that with a DLL. If they are doing it, then it's questionable to say the least about it.
    Friday, August 28, 2015 1:23 PM