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.
不久前,我在Microsoft Visual C#2008 Express Edition中编写了一个小程序。其中包含一个名为“ProblemReport.cs”的文件,其中包含自己的表单和ProblemReport类。
I'm writing a new program and want to reuse this code. (still working in MS Vis C# 2008 express)
我正在编写一个新程序,并希望重用此代码。 (仍在MS Vis C#2008快递中工作)
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.
在我的新程序中,在C#Solution Explorer中,我右键单击了我的项目并选择了“Add existing item ...”然后我添加了ProblemReport.cs,ProblemReport.designer.cs和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:
我在这里错过了哪些步骤?当我回到我的新程序中的主窗体,并尝试实例化“ProblemReport”的新实例时,我收到错误消息:
"The type of namespace name 'ProblemReport' could not be found (are you missing a using directive or an assembly reference?)"
“无法找到名称空间名称'ProblemReport'的类型(您是否缺少using指令或程序集引用?)”
Obviously - I'm supposed to do something else to get this to work... just not sure what it is!
显然 - 我应该做点别的事情才能让它发挥作用...只是不确定它是什么!
-Adeena
4 个解决方案
#1
Ensure that all your CS files are within the same namespace. Sounds like the imported one(s) aren't living with your new project.
确保所有CS文件都在同一名称空间内。听起来像进口的人没有和你的新项目一起生活。
Ensure that:
namespace MyProgram //same as the rest of your project
{
public class ProblemReport
{
}
}
meanwhile in your other .cs files....
同时在你的其他.cs文件....
namespace MyProgram
{
public class SomeClass
{
public void DoSomething()
{
ProblemReport. //you should get intellisense here.
}
}
}
#2
You might be better off extracting ProblemReport into it's own class library (separate dll) so you only have one copy of the code.
您可能最好将ProblemReport解压缩到它自己的类库(单独的dll)中,这样您只有一个代码副本。
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.
创建一个新的类库项目,将ProblemReport.cs,ProblemReport.designer.cs和ProblemReport.resx复制到该项目中,并根据需要更新名称空间。构建它并将dll置于中心位置。然后从两个程序中添加对此类库的引用。
#3
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.
然后您应该能够访问ProblemReport类。当然,这取决于ProblemReport.cs文件的命名空间名称。
#4
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?
在.CS文件中,命名空间是否与项目的其余部分匹配?
#1
Ensure that all your CS files are within the same namespace. Sounds like the imported one(s) aren't living with your new project.
确保所有CS文件都在同一名称空间内。听起来像进口的人没有和你的新项目一起生活。
Ensure that:
namespace MyProgram //same as the rest of your project
{
public class ProblemReport
{
}
}
meanwhile in your other .cs files....
同时在你的其他.cs文件....
namespace MyProgram
{
public class SomeClass
{
public void DoSomething()
{
ProblemReport. //you should get intellisense here.
}
}
}
#2
You might be better off extracting ProblemReport into it's own class library (separate dll) so you only have one copy of the code.
您可能最好将ProblemReport解压缩到它自己的类库(单独的dll)中,这样您只有一个代码副本。
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.
创建一个新的类库项目,将ProblemReport.cs,ProblemReport.designer.cs和ProblemReport.resx复制到该项目中,并根据需要更新名称空间。构建它并将dll置于中心位置。然后从两个程序中添加对此类库的引用。
#3
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.
然后您应该能够访问ProblemReport类。当然,这取决于ProblemReport.cs文件的命名空间名称。
#4
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?
在.CS文件中,命名空间是否与项目的其余部分匹配?