views:

1082

answers:

4

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

+4  A: 

Ensure that all your CS files are within the same namespace. Sounds like the imported one(s) aren't jiving 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.
            }
        }

    }
p.campbell
That was it... thanks! :)
adeena
+1  A: 

When you created it it's likely it used a different namespace, as those are generated from the project name by default.

In the .CS file does the namespace match that of the rest of your project?

blowdart
+2  A: 

You have to add a "using" directive to your new project -

using ProblemReport;

Then you should be able to access the ProblemReport class. Of course this depends on the name of namespace of the ProblemReport.cs file.

jkottnauer
+1; I tried to recreate the scenario and got the same message as adeena. Adding a using statement with the namespace for the imported form fixed the problem.
Fredrik Mörk
+3  A: 

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.

ChrisF
yes - you're right that this is what I should do... and I might... the namespace issue fixed it for now. The only reason why I wouldn't do this is that the form used in both of my programs will be a little different.
adeena
Well you could base class in the common code and sub-class in the programs...
ChrisF