2016-04-07 15 views
0

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 
+0

Should gerçek CustomerID'deki gibi daha az soyut 'olmak Id' şey, bunu taşındı? – Plutonix

+0

Evet, bu muhtemelen daha iyi olurdu, ancak bu aynı zamanda müşteri nesnesinde daha az veri tasarrufu sağlıyor mu? –

+0

Hayır, bir int, oluşturduğunuz ve artırdığınız int ile aynı "maliyeti" gösterecektir. – Plutonix

cevap

0

Figured out out.

Müşteri Sınıfında sözlüğün paylaşılmasını halka açık olarak değiştirdim. Ayrıca, değişkenin değişkeni için Static kullanmak yerine. Ben Özel Paylaşılan yapılmış ve depolandığı whereever gelen fonksiyon dışı ve sınıf bildirimi alanının

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 
+0

numaralı veri tabanına bırakın. Kendi cevaplarınızı işaretlemeniz mümkün olabilir ve bu cevabın çalıştığını görmek için diğer kullanıcıların cevap aramasını sağlar. –

İlgili konular