none
Is one solution with multiple projects using the same class names in each project possible?

    Question

  • I have setup a solution and added 3 projects to that solution, I want to be able to treat the projects like separate projects but VS2015 will not allow me to add the same class in each project, it gives the error that the class already exists. I am using the "add class" from the menu popup after right click on the project in the solution manager I want to add the class to.
    Saturday, December 10, 2016 4:56 PM

Answers

  • In order to work around this inconvenience, try some different ways of adding the classes. For example, add files instead of classes (having the corresponding extensions according to programming language), using Add New Item dialog. Then type the class. Another solution is to add a different class, then rename it.

    If your projects represent libraries, then make sure that homonymous classes are private (not exported).

    • Marked as answer by Exempt_AK Sunday, December 11, 2016 11:50 PM
    Sunday, December 11, 2016 4:38 PM

All replies

  • Hi friend!

    Step

         -right click on the project > Add > new Item > Class . Put the name of the class e click in ok.

    I hope to have helped!

     

    Saturday, December 10, 2016 8:58 PM
  • The way I use the same Class in multiple projects within One Solution is to "Add as Link" the Class file source.

    Therefore just one version exists, but is present in each project.

    That is a way to keep different project using one unique source...

    Have a look at this msdn: https://msdn.microsoft.com/en-us/library/windows/apps/jj714082(v=vs.105).aspx

    Is that possible for your solution?

    Saturday, December 10, 2016 9:50 PM
  • I have setup a solution and added 3 projects to that solution, I want to be able to treat the projects like separate projects but VS2015 will not allow me to add the same class in each project, it gives the error that the class already exists. I am using the "add class" from the menu popup after right click on the project in the solution manager I want to add the class to.

    Has each of the Sub-Projects its own Namespace? If so, it should work with a class that has the same name as in one ore more of the other projects.

    Regards,

      Thorsten

    Saturday, December 10, 2016 11:06 PM
  • My goal is to not have the projects share the same classes only have the same names. I only want a single solution for the convenience of having the dependency folder setup and one of the projects is .lib that I use in the other two projects which are not related to each other.

    When I go thru the steps of...

      -right click on the project > Add > new Item > Class

    it gives the error that the file exists in the project but it only exists in the other unrelated project.

    If it helps to understand my issue I have a game engine in my solution which is compiled as a .lib. I use that and several other dependency in each project that will use the game engine. I like to use a common class to create a game called Game but everything in the class will vary from game to game so sharing that class wouldn't work. Also I want to keep one solution because I add the game engine project as a "project dependency" for my game projects so it'll compile changes to the engine and then the games nicely.


    • Edited by Exempt_AK Saturday, December 10, 2016 11:58 PM
    Saturday, December 10, 2016 11:57 PM
  • I have setup a solution and added 3 projects to that solution, I want to be able to treat the projects like separate projects but VS2015 will not allow me to add the same class in each project, it gives the error that the class already exists. I am using the "add class" from the menu popup after right click on the project in the solution manager I want to add the class to.

    Has each of the Sub-Projects its own Namespace? If so, it should work with a class that has the same name as in one ore more of the other projects.

    Regards,

      Thorsten

    I really don't want to give them a namespace, it would just be a pain to work with, I think.

    Sunday, December 11, 2016 12:00 AM
  • It seems you are not following a proprer scheme for programming as your classes have the same name but not the same content as you do not want to share the content between project.

    I don't see a solution combining Classes using the same name but not the same code and without using a different namespace.

    Sunday, December 11, 2016 1:28 PM

  • I really don't want to give them a namespace, it would just be a pain to work with, I think.

    Hi,

    if you create a Solution, save that and then add new projects, these new projects should automatically run under their own namespace. This is the default behaviour...

    Regards,

      Thorsten

    Sunday, December 11, 2016 3:32 PM
  • In order to work around this inconvenience, try some different ways of adding the classes. For example, add files instead of classes (having the corresponding extensions according to programming language), using Add New Item dialog. Then type the class. Another solution is to add a different class, then rename it.

    If your projects represent libraries, then make sure that homonymous classes are private (not exported).

    • Marked as answer by Exempt_AK Sunday, December 11, 2016 11:50 PM
    Sunday, December 11, 2016 4:38 PM
  • It seems you are not following a proprer scheme for programming as your classes have the same name but not the same content as you do not want to share the content between project.

    I don't see a solution combining Classes using the same name but not the same code and without using a different namespace.

    Why would two separate projects with only similar dependencies be required to share source code? A game like Tetris would have an entirely different main class then Pacman, but to keep things simple in each game I like to use a common name like "Game" for the main game object.

    I didn't really find a really good solution for this but just adding existing items does work, it's just not very ...fluid?

    Sunday, December 11, 2016 11:49 PM

  • I really don't want to give them a namespace, it would just be a pain to work with, I think.

    Hi,

    if you create a Solution, save that and then add new projects, these new projects should automatically run under their own namespace. This is the default behaviour...

    Regards,

      Thorsten


    This doesn't seem to be the case for me as each new project I've made gives the same error that the files exist when creating a new class. (The file only exist in the old projects ot the new empty ones.)
    Sunday, December 11, 2016 11:50 PM
  • This is sort of how I got around my issue, added existing files. Only one project is a library and it's the "game engine" all other projects are just games using the engine, makes it easy to work with both at once.
    Sunday, December 11, 2016 11:52 PM