Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a cs file to an existing C# project?

A while back I wrote a little program in Microsoft Visual C# 2008 Express Edition. In it included a file called "ProblemReport.cs" with it's own form and ProblemReport class.

I'm writing a new program and want to reuse this code. (still working in MS Vis C# 2008 express)

In my new program, in the C# Solution Explorer, I right clicked on my project and chose "Add existing item..." I then added ProblemReport.cs, ProblemReport.designer.cs and ProblemReport.resx.

What step(s) am I missing here? When I go back to the main form in my new program, and try to instantiate a new instance of "ProblemReport", I get the error message:

"The type of namespace name 'ProblemReport' could not be found (are you missing a using directive or an assembly reference?)"

Obviously - I'm supposed to do something else to get this to work... just not sure what it is!

-Adeena

like image 241
adeena Avatar asked May 23 '09 14:05

adeena


2 Answers

Ensure that all your CS files are within the same namespace. Sounds like the imported one(s) aren't living with your new project.

Ensure that:

namespace MyProgram //same as the rest of your project
{

    public class ProblemReport
    {
    }

}

meanwhile in your other .cs files....

namespace MyProgram 
{

     public class SomeClass
     {
           public void DoSomething()
           {
               ProblemReport. //you should get intellisense here.
           }
      }

}
like image 85
p.campbell Avatar answered Sep 18 '22 05:09

p.campbell


You might be better off extracting ProblemReport into it's own class library (separate dll) so you only have one copy of the code.

Create a new class library project, copy ProblemReport.cs, ProblemReport.designer.cs and ProblemReport.resx into that and update the namespace as required. Build it and put the dll somewhere central. Then add a reference to this class library from your two programs.

like image 31
ChrisF Avatar answered Sep 18 '22 05:09

ChrisF