vb.net “form”是“project”中的一种类型,不能用作表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17686914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:17:22  来源:igfitidea点击:

'form' is a type in 'project' and cannot be used as an expression

vb.netprojectmulti-project

提问by TheRyan722

I have a multi project solution in VB.Net. I have a custom made form, which other forms can inherit. It is in a seperate project called "CustomForm", there are no special graphical effects, it is the same as a generic Windows Form, just handles closing differently. I have a project called "TestProject1" with a form named Form1 in it, which inherits CustomForm. In the third project "TestManager", you can add an instance of Form1 from TestProject1, and set its ownership to TestManager. I have this setup with the following code inside TestManager:

我在 VB.Net 中有一个多项目解决方案。我有一个定制的表单,其他表单可以继承。它在一个名为“CustomForm”的单独项目中,没有特殊的图形效果,它与通用的 Windows Form 相同,只是处理关闭不同。我有一个名为“TestProject1”的项目,其中有一个名为 Form1 的表单,它继承了 CustomForm。在第三个项目“TestManager”中,您可以从TestProject1 中添加Form1 的一个实例,并将其所有权设置为TestManager。我在 TestManager 中使用以下代码进行了此设置:

Public Shared Sub CreateForm(ByVal frm As CustomForm.CustomForm)

    frm.Owner = TestManager.TestManager

    frm.Show()
End Sub

However I get the following error: 'Form1' is a type in 'TestProject1' and cannot be used as an expression.

但是我收到以下错误:“Form1”是“TestProject1”中的一种类型,不能用作表达式。

EDIT: More Details:

编辑:更多细节:

Form1 has nothing on it at the moment. Imagine TestManager as a desktop, where a form from another project is added to it. TestManager references TestProject1, and used the code: CreateForm(TestProject1.Form1) which utilizes the above method. Now form1 references and inherits CustomForm. This error is displayed the moment I enter the code, so I cannot even build the project.

Form1 目前没有任何内容。将 TestManager 想象成一个桌面,其中添加了来自另一个项目的表单。TestManager 引用了TestProject1,并使用了利用上述方法的代码:CreateForm(TestProject1.Form1)。现在 form1 引用并继承了 CustomForm。这个错误在我输入代码的那一刻就显示出来,所以我什至无法构建项目。

回答by TheRyan722

Due to my own stupidity, I oversaw such a simple error. I did not create an object or instance of the form, which was the issue. Simply had to add 'New' to the line.

由于我自己的愚蠢,我监督了这样一个简单的错误。我没有创建表单的对象或实例,这是问题所在。只需在行中添加“新建”即可。

CreateForm(New TestProject1.Form1())

回答by Compa

Just a little help here. An internship at my office was having this issue. The reason: he was overloading the constructor (wich was ok) but he did NOT create a default constructor.

这里只是一点帮助。我办公室的一次实习遇到了这个问题。原因:他重载了构造函数(这很好)但他没有创建默认构造函数。

It seems that if you want to use a class of your own without sending parameters, it's ok, but if you want to have more than one constructor and you DO NOT add the default constructor (the one without parameters) then this error will rise.

看起来,如果你想使用自己的类而不发送参数,也可以,但是如果你想拥有多个构造函数并且你不添加默认构造函数(没有参数的构造函数),那么这个错误就会上升。

He was using VS 2010 Pro

他使用的是 VS 2010 Pro