none
#include for c# RRS feed

  • Question

  • I have two projects which share information in the registry; so they both need to have a Form, some variables and some routines which are identical.

    I have a MyBitsAndPieces.cs file which contains the Form in a "MyBitsAndPieces" namespace which works fine when I click the VS19 menu item "Project/Add Existing Item", choose this MyBitsAndPieces.cs file and click "Add As Link" in each project.

    But in the absence of a #include directive how can I put the variables and routines in a shared source file?
    Saturday, July 27, 2019 10:56 AM

Answers

  • Hello,

    Now using "Add as Link" in each project is fine but if you happen to make changes in that class project it of course can have adverse affects which may not be okay. Either way, you still need to use a "using" (as there is no include directive in C#, only C++) directive to access that project's namespace and classes within.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Pete LakerMVP, Editor Saturday, July 27, 2019 1:31 PM
    • Marked as answer by MyKeg Tuesday, August 6, 2019 8:46 AM
    Saturday, July 27, 2019 11:06 AM
    Moderator

All replies

  • Hello,

    Now using "Add as Link" in each project is fine but if you happen to make changes in that class project it of course can have adverse affects which may not be okay. Either way, you still need to use a "using" (as there is no include directive in C#, only C++) directive to access that project's namespace and classes within.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Pete LakerMVP, Editor Saturday, July 27, 2019 1:31 PM
    • Marked as answer by MyKeg Tuesday, August 6, 2019 8:46 AM
    Saturday, July 27, 2019 11:06 AM
    Moderator
  • But in the absence of a #include directive how can I put the variables and routines in a shared source file?

    Why can't you use  a classlib project, an assembly that can be shared with other assemblies such as an programname.exe?

    https://www.mindstick.com/Articles/980/creating-c-sharp-class-library-dll-using-visual-studio-dot-net

    Saturday, July 27, 2019 12:38 PM
  • Class libraries are the same concept in Vb and C#.

    Karen is correct that it is usually referred to in a "using" statement. 

    You want it tightly coupled through assembly interfaces, to avoid framework mismatches too.

    Sharing a CS file with two projects is not cool. Which one does it check into repo with? What if they diverge? Reinstall on a new machine with different folder structure, etc.

    I agree shared code should be in a shared library project (dll), which you refer to in your project as a project reference, then a using statement where used. So you are both talking the same thing I think.

    Are you happy to go with Karen's advice?

    All the best,
    Pete


    #PEJL
    Got any nice code? If you invest time in coding an elegant, novel or impressive answer on MSDN forums, why not copy it over to TechNet Wiki, for future generations to benefit from! You'll never get archived again, and you could win weekly awards!

    Have you got what it takes o become this month's TechNet Technical Guru? Join a long list of well known community big hitters, show your knowledge and prowess in your favoured technologies!


    Saturday, July 27, 2019 1:31 PM
    Moderator