错误:“'新'不能在接口上使用” [英] Error: "'New' cannot be used on an interface"

查看:173
本文介绍了错误:“'新'不能在接口上使用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我收到了这个错误。



Hi!

I was getting this error.

Private Sub DATAGRIDVIEW_TO_EXCEL(ByVal DGV As DataGridView)
        Dim f As FolderBrowserDialog = New FolderBrowserDialog
        Try
            If f.ShowDialog() = DialogResult.OK Then 
                'This section help you if your language is not English.
                System.Threading.Thread.CurrentThread.CurrentCulture = _
                System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
                Dim oExcel As Excel.Application
                Dim oBook As Excel.Workbook
                Dim oSheet As Excel.Worksheet
                oExcel = CreateObject("Excel.Application")
                oBook = oExcel.Workbooks.Add(Type.Missing)
                oSheet = oBook.Worksheets(1)

                Dim dc As System.Data.DataColumn
                Dim dr As System.Data.DataRow
                Dim colIndex As Integer = 0
                Dim rowIndex As Integer = 0

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

                For CLS = 0 To DGV.ColumnCount - 1 ' COLUMNS OF DTB
                    DTB.Columns.Add(DGV.Columns(CLS).Name.ToString)
                Next

                Dim DRW As DataRow

                For RWS = 0 To DGV.Rows.Count - 1 ' FILL DTB WITH DATAGRIDVIEW
                    DRW = DTB.NewRow

                    For CLS = 0 To DGV.ColumnCount - 1
                        Try
                            DRW(DTB.Columns(CLS).ColumnName.ToString) = DGV.Rows(RWS).Cells(CLS).Value.ToString
                        Catch ex As Exception

                        End Try
                    Next

                    DTB.Rows.Add(DRW)
                Next


                'Export the Columns to excel file
                For Each dc In DTB.Columns
                    colIndex = colIndex + 1
                    oSheet.Cells(1, colIndex) = dc.ColumnName
                Next

                'Export the rows to excel file
                For Each dr In DTB.Rows
                    rowIndex = rowIndex + 1
                    colIndex = 0
                    For Each dc In DTB.Columns
                        colIndex = colIndex + 1
                        oSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
                    Next
                Next

                'Set final path
                Dim fileName As String = "\ExportedAuthors" + ".xls"
                Dim finalPath = f.SelectedPath + fileName
                oSheet.Columns.AutoFit()
                'Save file in final path
                oBook.SaveAs(finalPath, XlFileFormat.xlWorkbookNormal, Type.Missing, _
                Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, _
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)

                'Release the objects
                ReleaseObject(oSheet)
                oBook.Close(False, Type.Missing, Type.Missing)
                ReleaseObject(oBook)
                oExcel.Quit()
                ReleaseObject(oExcel)
                'Some time Office application does not quit after automation: 
                'so i am calling GC.Collect method.
                GC.Collect()

                MessageBox.Show("Export done successfully!")

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK)
        End Try

    End Sub





有人可以解释一下吗?谢谢



Can someone explain please? Thank you

推荐答案

添加使用语句

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


这篇关于错误:“'新'不能在接口上使用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆