Vb .Net在按钮点击时将项目添加到字典

时间:2016-10-25 07:52:19

标签: vb.net dictionary

所以我有2个表单和一个类,在我的第一个表单上,它将项添加到字典中,在我的第二个表单上,它检查应该添加到字典上的项是否成功。但奇怪的是,在表单1中,它显示该项目已添加到字典中,但在我的第二种形式中,当我得到字典计数时,它产生0,这意味着那里没有任何内容。希望你能帮助我。

表格1

Public Class register

Dim acc_num As New System.Text.StringBuilder()
Dim account_info As New ArrayList()
Dim access_acc As New Accounts

Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
    Try
        account_info.Add(fname_txt.Text & " " & lname_txt.Text)
        account_info.Add("0.00")
        access_acc.addAcc(account_number_lbl.Text, account_info)
        MsgBox("Your account has been registered! Thank you for banking with us, your money is in good hands")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
 End Sub

End Class

表格2

Dim access_acc As New Accounts
 Private Sub btn_process_Click(sender As Object, e As EventArgs) Handles btn_process.Click
    MsgBox(access_acc.getCount().ToString)
 End Sub

我的班级

Public Class Accounts

Private account As New Dictionary(Of String, ArrayList)

Public Sub addAcc(ByVal account_number As String, account_info As ArrayList)

    account.Add(account_number, account_info)
    MsgBox(account.Count)

End Sub

   Public Function getCount() As Integer
    Return account.Count
End Function

  End Class

1 个答案:

答案 0 :(得分:0)

我通过使用Module(它是静态的C#对应物)而不是Class来摆脱我的问题。 以下是在特定情况下使用的最佳方法的参考。

Module VS Classes