且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误:“'新'不能在接口上使用”

更新时间:2023-02-04 12:37:30

添加使用语句

add using statement
using System.Windows.Forms;
using System.Data



或使用完整的班级名称


or use full class name

Dim f As System.Windows.Forms.FolderBrowserDialog = New System.Windows.Forms.FolderBrowserDialog






And

Dim DTB As New System.Data.DataTable


改为使用它:

Use this instead:
Imports System.Data



    Dim dt As New System.Data.DataTable





问题可能是编译器将DataTable与Excel命名空间中的某个同名接口混淆。如果您没有使用Excel,则不会出现此问题。



The problem is probably the compiler confusing DataTable with some interface of the same name in the Excel namespace. If you didn't use Excel, you wouldn't have this problem.


'Fill data to datatable
                Dim DTB As New DataTable <--- Error occurs here



Dim语句(Visual Basic)在将变量声明为接口类型时使用New(Visual Basic)子句。

虽然接口是引用类型,但您无法创建它的实例。您可以使用New来创建类或结构的实例。





通常在DataTable情况下,命名空间之间发生冲突。更好地使用完整命名空间,看看它是否会再次抛出错误。



Dim dt As New System.Data.DataTable


A Dim Statement (Visual Basic) uses a New (Visual Basic) clause when declaring a variable to be of an interface type.
Although an interface is a reference type, you cannot create an instance of it. You can use New only to create an instance of a class or a structure.


Normally in DataTable case it's conflicts happening between the namespace. better use full namespace and see if it again throws error.

Dim dt As New System.Data.DataTable