Bir Sözlük'te depolanacak bir Müşteri Nesnesi yaratan bir sınıfa sahibim. Sözlük bir (Of TInteger, Müşteri) kullanır. Yeni bir müşteri eklendiğinde Bir kullanıcının bir müşteri aramak için kullanabileceği benzersiz bir kimliği olmalıdır. Sözlük Anahtarını ID olarak kullanmayı planlıyorum.Sözlük anahtarı olarak kullanılacak tamsayı değişkeni artırmak için sınıf işlevini nasıl edinebilirim
AddCustomer formumda addCustomer düğmesini tıklattığınızda. Müşteri nesnesini oluşturur. Sonra bu nesneyi sözlük değerine koyar. Anahtar, statik bir değişken oluşturan ve ayarlayan Müşteri sınıfındaki bir işlevi çağırarak ayarlanır. Sonra 1 ekler ve döner. İşte
Sınıf (yardım için gerekli değildir eksi kodu) İşte nesnesi oluşturur ve çağrıları fonksiyonu ve ayarlar benim addCustomerForm benim kod
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
benim Müşteriler ise sözlük.
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
Should gerçek CustomerID'deki gibi daha az soyut 'olmak Id' şey, bunu taşındı? – Plutonix
Evet, bu muhtemelen daha iyi olurdu, ancak bu aynı zamanda müşteri nesnesinde daha az veri tasarrufu sağlıyor mu? –
Hayır, bir int, oluşturduğunuz ve artırdığınız int ile aynı "maliyeti" gösterecektir. – Plutonix