Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将cs文件添加到现有的C#项目中?_C#_Visual Studio 2008_File - Fatal编程技术网

如何将cs文件添加到现有的C#项目中?

如何将cs文件添加到现有的C#项目中?,c#,visual-studio-2008,file,C#,Visual Studio 2008,File,不久前,我用Microsoft Visual C#2008快速版编写了一个小程序。其中包括一个名为“ProblemReport.cs”的文件,它有自己的表单和ProblemReport类 我正在编写一个新程序,希望重用此代码。(仍在MS Vis C#2008 express工作) 在我的新程序中,在C#解决方案资源管理器中,我右键单击我的项目并选择“添加现有项…”,然后添加ProblemReport.cs、ProblemReport.designer.cs和ProblemReport.resx

不久前,我用Microsoft Visual C#2008快速版编写了一个小程序。其中包括一个名为“ProblemReport.cs”的文件,它有自己的表单和ProblemReport类

我正在编写一个新程序,希望重用此代码。(仍在MS Vis C#2008 express工作)

在我的新程序中,在C#解决方案资源管理器中,我右键单击我的项目并选择“添加现有项…”,然后添加ProblemReport.cs、ProblemReport.designer.cs和ProblemReport.resx

我在这里遗漏了什么步骤?当我返回到新程序中的主窗体,并尝试实例化“ProblemReport”的新实例时,我收到错误消息:

“找不到命名空间名称“ProblemReport”的类型(是否缺少using指令或程序集引用?)

很明显,我应该做些别的事情来让它工作。。。只是不知道是什么


-Adeena

确保所有CS文件都位于同一名称空间中。听起来导入的项目与您的新项目不在一起

确保:

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

    public class ProblemReport
    {
    }

}
同时在您的其他.cs文件中

namespace MyProgram 
{

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

}

当您创建它时,它可能使用了不同的名称空间,因为默认情况下这些名称空间是从项目名称生成的

在.CS文件中,名称空间是否与项目其余部分的名称空间匹配?

您必须在新项目中添加一个“使用”指令-

using ProblemReport;

然后您应该能够访问ProblemReport类。当然,这取决于ProblemReport.cs文件的名称空间。

您最好将ProblemReport提取到它自己的类库(单独的dll)中,这样您就只有一个代码副本


创建一个新的类库项目,将ProblemReport.cs、ProblemReport.designer.cs和ProblemReport.resx复制到该项目中,并根据需要更新名称空间。构建它并将dll放在中心的某个位置。然后从两个程序中添加对此类库的引用。

+1;我试图重现这个场景,得到了和阿德娜一样的信息。为导入的表单添加名称空间为的using语句修复了问题。是的-您说得对,这是我应该做的。。。我可能。。。名称空间问题暂时解决了它。我不这么做的唯一原因是我的两个程序中使用的表单会有点不同。我认为这个答案需要更多的信息。当前的答案表明,所有内容都必须位于同一名称空间中。如果是这种情况,则不能在其他程序中重复使用文件。