7

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 Answers 4

10

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.
           }
      }

}
Sign up to request clarification or add additional context in comments.

1 Comment

I think this answer needs more information. The current answer suggests that everything has to be in the same namespace. If this were the case then you could not re-use a file in another program.
4

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.

1 Comment

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.
2

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.

1 Comment

+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.
1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.