对象引用未设置为对象的实例.OOP 类 vb.net



我正在尝试创建一个具有某些属性的函数类,但它一直说

对象引用未设置为对象的实例。

Public Class Class1    
    Private sParam As Dictionary(Of String, String)
    Public Property param As Dictionary(Of String, String)
        Set(ByVal value As Dictionary(Of String, String))
            sParam = value
        End Set
        Get
            Return sParam
        End Get
    End Property
    Public Sub insert(ByVal table As String)
        Dim col As String = ""
        Dim val As String = ""
        Dim init As Integer = 0
        For Each x In param
            If init = 0 Then
                col &= x.Key
                val &= x.Value
                init = 1
            Else
                col &= ", " & x.Key
                val &= ", " & x.Value
            End If
        Next
        MsgBox("INSERT INTO " & table & "(" & col & ") VALUES (" & val & ")")
    End Sub
End Class
Public Class Form1
    Private Sub Button1_Click(ByVal sender As Button, ByVal e As System.EventArgs) Handles Button1.Click
        Dim db As Class1
        db = New Class1
        db.param.Add("First_name", "John")
        db.param.Add("Last_name", "Doe")
        db.insert("tblUsers")
    End Sub
End Class
您需要

实例化Dictionary(Of String, String)对象:

Public Class Class1    
    Private sParam As New Dictionary(Of String, String)
                      '''
    ...
End Class

最新更新