不能用"New"声明数组
Public Class DBoperation
Dim conn As New OleDb.OleDbConnection
Dim cmd As New OleDb.OleDbCommand
Dim da As New OleDb.OleDbDataAdapter
Dim dt As New DataTable
Public Sub New(ByVal connstr As String)
conn.ConnectionString = connstr
cmd.Connection = conn
da.SelectCommand = cmd
End Sub
Public Function executesql(ByVal sql As String) As DataTable
If InStr(LCase(sql), "select") Then
cmd.CommandText = sql
conn.Open()
dt = New DataTable
da.Fill(dt)
conn.Close()
Return dt
Else
If InStr(LCase(sql), "delete") Or InStr(LCase(sql), "update") Or InStr(LCase(sql), "insert") Then
cmd.CommandText = sql
conn.Open()
If InStr(LCase(sql), "delete") Then
MsgBox("删除记录 " & cmd.ExecuteNonQuery())
End If
If InStr(LCase(sql), "update") Then
MsgBox("更新记录 " & cmd.ExecuteNonQuery())
End If
If InStr(LCase(sql), "insert") Then
MsgBox("新增记录 " & cmd.ExecuteNonQuery())
End If
conn.Close()
End If
Return Nothing
End If
End Function
End Class
这是我定义的一个类
然后
Public Class LendInfo
Dim dboperation1(connstr) As New DBoperation()
Private Sub LendInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sql As String = "select * from StudentInfo where ID = '" & stuno & "'"
Dim dt As New DataTable
dt = dboperation1.executesql(sql)
End Sub
End Class 其中connstr在公共变量模块中定义了。
为什么会提示:不能用"New"声明数组?
请各位大虾帮帮忙!!
[最优解释]
晕倒,如果不是定义数组,那下面这样就可以了
Dim dboperation1 As New DBoperation(connstr)
或者
Dim dboperation1 As DBoperation=New DBoperation(connstr)
你那方式不对
dboperation1 是变量的名称,你只能对它进行赋值,它又不是方法,可以接受你传的参数
[其他解释]
楼上的讲解很正确,对于一个数组,我们在定义的时候仅仅是定义了一个什么类型的,以及他的长度,并没有对数组中的每一个个体进行实例化,所以必须要对每一个个体进行实例化,才可以调用!
[其他解释]
Dim dboperation1(connstr) As DBoperation
去掉New就可以了
或者像下面这样声明
Dim dboperation1() As DBoperation=New DBoperation(connstr+1){}
[其他解释]
[其他解释]