如何获得 class 函数来递增整数变量以用作字典键
How do I get a class function to increment integer variable to be used as a dictionary key
我有一个 class 创建一个客户对象,该对象将存储在字典中。字典使用 (Of TInteger, Customer)。
添加新客户时需要有一个唯一的 ID,用户可以使用它来搜索客户。我打算使用字典键作为 ID。
当我的 addCustomer 表单单击 addCustomer 按钮时。它创建客户对象。然后将该对象放入字典值中。密钥是通过调用 Customer class 中的一个函数来设置的,该函数创建并设置一个静态变量。然后加 1 和 returns.
这是我的客户 Class(减去不需要帮助的代码)
Public Class Customers
'Create a dictionary to hold the new customer
Friend customerDict As New Dictionary(Of Integer, Customers)
Private cFirstName As String 'first name
Private cLastName As String 'last name
Private cAddress As String 'field to address
Private cContactInfo As String 'contact information i.e. a telephone number Or an e-mail address
Private cCountry As Boolean 'country the customer lives In
Friend cMortgage As List(Of Mortgage) 'customer has one or more mortgages
Public Sub New(cFirstName1 As String, cLastName1 As String, cAddress1 As String, cContactInfo1 As String, cCountry1 As Boolean)
Me.CFirstName1 = cFirstName1
Me.CLastName1 = cLastName1
Me.CAddress1 = cAddress1
Me.CContactInfo1 = cContactInfo1
Me.CCountry1 = cCountry1
cMortgage = addCMortgage()
End Sub
......
Code for getters and setters
......
'Function to create a new customer ID/Key
Public Function getCustomerID() As Integer
'Create variable that adds 1 everytime a customer is created. Then use that as customer Id and as the Customer Dictionary Key.
Static cIdKey As Integer = 0
cIdKey += 1
Return cIdKey
End Function
End Class
这是我的 addCustomerForm 代码,它创建对象并调用函数并设置字典。
Public Class frmAddNewCustomer
Private Sub frmAddNewCustomer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnAddMortgage_Click(sender As Object, e As EventArgs) Handles btnAddMortgage.Click
Dim cCountry As Boolean
Dim cAddress As String
......'Set up address format and code validation.....
'Create the Customer object
Dim Customers As New Customers(txtfName.ToString, txtLName.ToString, cAddress, txtContact.ToString, cCountry)
'Create a customers Dictionary to store the Customer.
Customers.customerDict.Add(Customers.getCustomerID, Customers)
'Close the add customer form
Me.Close()
End Sub
End Class
想通了。
在客户 Class 中,我将字典的声明更改为 public 共享。此外,而不是使用 Static 来使变量递增。我将它设为 Private Shared 并将其移出函数并移至 class 声明区域
Public Class Customers
'Create a dictionary to hold the new customer
Public Shared customerDict As New Dictionary(Of Integer, Customers)
Private Shared cIdKey As Integer = 0
我有一个 class 创建一个客户对象,该对象将存储在字典中。字典使用 (Of TInteger, Customer)。 添加新客户时需要有一个唯一的 ID,用户可以使用它来搜索客户。我打算使用字典键作为 ID。
当我的 addCustomer 表单单击 addCustomer 按钮时。它创建客户对象。然后将该对象放入字典值中。密钥是通过调用 Customer class 中的一个函数来设置的,该函数创建并设置一个静态变量。然后加 1 和 returns.
这是我的客户 Class(减去不需要帮助的代码)
Public Class Customers
'Create a dictionary to hold the new customer
Friend customerDict As New Dictionary(Of Integer, Customers)
Private cFirstName As String 'first name
Private cLastName As String 'last name
Private cAddress As String 'field to address
Private cContactInfo As String 'contact information i.e. a telephone number Or an e-mail address
Private cCountry As Boolean 'country the customer lives In
Friend cMortgage As List(Of Mortgage) 'customer has one or more mortgages
Public Sub New(cFirstName1 As String, cLastName1 As String, cAddress1 As String, cContactInfo1 As String, cCountry1 As Boolean)
Me.CFirstName1 = cFirstName1
Me.CLastName1 = cLastName1
Me.CAddress1 = cAddress1
Me.CContactInfo1 = cContactInfo1
Me.CCountry1 = cCountry1
cMortgage = addCMortgage()
End Sub
......
Code for getters and setters
......
'Function to create a new customer ID/Key
Public Function getCustomerID() As Integer
'Create variable that adds 1 everytime a customer is created. Then use that as customer Id and as the Customer Dictionary Key.
Static cIdKey As Integer = 0
cIdKey += 1
Return cIdKey
End Function
End Class
这是我的 addCustomerForm 代码,它创建对象并调用函数并设置字典。
Public Class frmAddNewCustomer
Private Sub frmAddNewCustomer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnAddMortgage_Click(sender As Object, e As EventArgs) Handles btnAddMortgage.Click
Dim cCountry As Boolean
Dim cAddress As String
......'Set up address format and code validation.....
'Create the Customer object
Dim Customers As New Customers(txtfName.ToString, txtLName.ToString, cAddress, txtContact.ToString, cCountry)
'Create a customers Dictionary to store the Customer.
Customers.customerDict.Add(Customers.getCustomerID, Customers)
'Close the add customer form
Me.Close()
End Sub
End Class
想通了。
在客户 Class 中,我将字典的声明更改为 public 共享。此外,而不是使用 Static 来使变量递增。我将它设为 Private Shared 并将其移出函数并移至 class 声明区域
Public Class Customers
'Create a dictionary to hold the new customer
Public Shared customerDict As New Dictionary(Of Integer, Customers)
Private Shared cIdKey As Integer = 0