الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

نقاش Customer-OOP-Calss

مغلق
بدأه ابوأنس في 18 ديسمبر 2005 · 14 رد · 952 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

منذ فترة وأنا أحاول تحويل أسلوب برمجتى الى OOP البرمجة الهدفية أو الشئية وكتبت هذا الصنف كمثال ولكن لدي بعض التساؤلات

1- إذا كان CustID يمكن القراءة كيف يمكنني أن أضع فيه قيمة من خلال CustomersCollection Class

2-كيف يمكنني منع إستخدام كلمة new عند تعريف متغير جديد من Customer كما هو الحال في DataRow

3- السؤال لأهم: هل أسلوبي صحيح؟؟؟

وسأتي بقية الأسئلة تباعا

Namespace Customers
    Public Class Customer
        Private _CustID As Integer
        Public ReadOnly Property CustID() As Integer
            Get
                Return _CustID
            End Get
        End Property

        Public Sub Set_CustID(ByVal CustID As Integer)
            _CustID = CustID
        End Sub

        Private _CustName As String
        Public Property CustName() As String
            Get
                Return _CustName
            End Get
            Set(ByVal Value As String)
                If Value = Nothing Then Throw New NullReferenceException("Name Can't be Null")
                _CustName = Value
            End Set
        End Property

        Private _CustPhone As String
        Public Property CustPhone() As String
            Get
                Return _CustPhone
            End Get
            Set(ByVal Value As String)
                If Value = Nothing Then Throw New NullReferenceException("Phone Can't be Null")
                _CustPhone = Value
            End Set
        End Property

        Private _CustNotes As String
        Public Property CustNotes() As String
            Get
                Return _CustNotes
            End Get
            Set(ByVal Value As String)
                _CustNotes = Value
            End Set
        End Property

    End Class

    Public Class CustomesCollection
        Private CollTable As DataTable

        Sub New()
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'write here a Code For Getting the Data From the Real Table
            'into a DataTable (CollTable)
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'Creating the Table
            CollTable = New DataTable("Customers")
            CollTable.Columns.Add("CustID", GetType(Integer))
            CollTable.Columns.Add("CustName", GetType(String))
            CollTable.Columns.Add("CustPhone", GetType(String))
            CollTable.Columns.Add("CustNotes", GetType(String))

            'Adding a Sample Data to it 
            Dim row As DataRow = CollTable.NewRow
            row.ItemArray = New Object() {1, "Khalid", "999", ""}
            CollTable.Rows.Add(row)
            row = CollTable.NewRow
            row.ItemArray = New Object() {2, "Ahmmad", "7777", ""}
            CollTable.Rows.Add(row)
            row = CollTable.NewRow
            row.ItemArray = New Object() {3, "Omar", "2222", "Some Notes"}
            CollTable.Rows.Add(row)


        End Sub

        'returns Number of Items in the Collection 
        'by Counting the rows in CollTable
        Public ReadOnly Property Count() As Integer
            Get
                Return CollTable.Rows.Count
            End Get
        End Property


        Public ReadOnly Property DataSource() As DataTable
            Get
                Return CollTable.Copy
            End Get
        End Property

        Public ReadOnly Property FilteredDataSource() As DataTable
            Get
                Return CollTable
            End Get
        End Property

        'returns All Items in the Collection by Converting the rows 
        'in the CollTable To Customer Object Array
        Public ReadOnly Property Items() As Customer()
            Get
                Dim Coll As New ArrayList
                For Each row As DataRow In CollTable.Rows
                    Coll.Add(ConvertRowToCustomer(row))
                Next
                Return Coll.ToArray(GetType(Customer))
            End Get
        End Property

        'returns Items after filtring the Items 
        Public ReadOnly Property Items(ByVal RowFilter As String) As Customer()
            Get
                Dim Coll As New ArrayList
                Dim dv As DataView = CollTable.DefaultView
                dv.RowFilter = RowFilter
                If dv.Count > 0 Then
                    For i As Integer = 0 To dv.Count - 1
                        Coll.Add(ConvertRowToCustomer(dv.Item(i).Row))
                    Next
                    Return Coll.ToArray(GetType(Customer))
                End If
                Return Nothing
            End Get
        End Property

        'returns an Item by the index
        Public ReadOnly Property Item(ByVal indx As Integer) As Customer
            Get
                If indx >= CollTable.Rows.Count Then Throw New IndexOutOfRangeException("Can't find index " + indx.ToString() + " in the Collection")
                Return ConvertRowToCustomer(CollTable.Rows(indx))
            End Get
        End Property

        'Add A new Customer object to the Collection
        Public Sub Add(ByVal Cust As Customer)
            If Cust Is Nothing Then Throw New NullReferenceException
            If Cust.CustID <> Nothing Then
                '(updating) remove old Customer 
                Remove(Cust)
            Else
                '(New) setting the new ID 
                Cust.Set_CustID(NewID)
            End If
            CollTable.Rows.Add(ConvertCustomerToRow(Cust))
        End Sub

        'Overloading on Add 
        Public Sub Add(ByVal Name As String, ByVal Phone As String)
            Dim Cust As New Customer
            Cust.CustName = Name
            Cust.CustPhone = Phone
            Cust.CustNotes = ""
            Add(Cust)
        End Sub

        'Deleting a Customer Object from Collection
        Public Sub Remove(ByVal Cust As Customer)
            Dim dv As DataView = CollTable.DefaultView
            dv.RowFilter = "CustID=" + Cust.CustID.ToString()
            If dv.Count = 0 Then Throw New NullReferenceException("Object does not have a reference")
            dv.Item(0).Row.Delete()
        End Sub

        'Deleting a Customer Object from Collection at spcefice index
        Public Sub RemoveAt(ByVal indx As Integer)
            CollTable.Rows(0).Delete()
        End Sub

        'Converting a DataRow from CollTable To A Customer Object
        Private Function ConvertRowToCustomer(ByVal row As DataRow) As Customer
            Dim Cust As New Customer
            Cust.Set_CustID(row.Item("CustID"))
            Cust.CustName = row.Item("CustName")
            Cust.CustPhone = row.Item("CustPhone")
            Cust.CustNotes = row.Item("CustNotes")
            Return Cust
        End Function

        'Converts a Customer Object into a DataRow
        Private Function ConvertCustomerToRow(ByVal Cust As Customer) As DataRow
            Dim row As DataRow = CollTable.NewRow()
            row.Item("CustID") = Cust.CustID
            row.Item("CustName") = Cust.CustName
            row.Item("CustPhone") = Cust.CustPhone
            row.Item("CustNotes") = Cust.CustNotes
            Return row
        End Function

        'returns New Customer ID for Inserting 
        Private Function NewID() As Integer
            Dim Max As Integer = 0
            For Each row As DataRow In CollTable.Rows
                If row.Item("CustID") > Max Then
                    Max = row.Item("CustID")
                End If
            Next
            Return Max + 1
        End Function
    End Class
End Namespace

CustomerOOPExample.rar

#2

ربما تستطيع مساعدتنا أكثر في تعريف وظيفة كل فئة من هذه الفئتين.

الفئة Customer كما يبدو وظيفتها حفظ معلومات عن عميل معين. الغريب في الأمر عدم وجود منشئ أو صانع يجعلك قادرا على صنع واحدا!

public class Customer
public New(id as Integer, name as String.....)
CustID=id
CustName=name
End Sub

بهذا لن تحتاج إلى أمر مثل setID الذي عندك!! لاحظ أنك تريدها للقراءة فقط، لكنك بطريقة غير مباشرة جعلت منها للكتابة أيضا،، (إن كان ولا بد أن تعيد تعريف قيمة id فعليك بجعل الكتابة private وليست public! بهذا تجعل فقط الفئات القريبة مثل CustomerCollection فقط بصلاحية تغيير ID..

وظيفة الفئة الأخرى أيضا غير واضحة؟ لكن يبدو أن وظيفته حفظ بيانات العملاء في مكان ما!

أعتقد أنك تحتاج إلى شفرة نظيفة مثل هذه:

public interface CustomerDataSource
public abstract Function getCustomers() as Customer()
public abstract Function getCustomerByID(is as Integer) as Customer
public abstract Function addCustomer(customer as Customer)
public abstract Function removeCustomerByID(id as Integer)
public abstract Function removeCustomer(c as Customer)

لاحظ هذه الأمور:

أن هذه واجهة، عليك باستخدامها في البرنامج بصفة أساسية.

أن CustID في الفئة Customer تستطيع تعريفها فقط في New وهي في النهاية للقراءة فقط! ولن تحتاج إلى setID!

أن العمليات التي أضفناها في interface هي عمليات أساسية كما أرى (تستطيع تغييرها حسب حاجتك)

بعد ذلك تصنع فئة تنفذ الخطة التي وضعناها:

public Class CustomerCollection implements CustomerDataSource
//code

ربما توجد أخطاء في شفراتي لكن أعتقد أنك تستطيع فهمها بسهولة.

بالتوفيق!

#4

عفوا على التأخير

بالنسبة للكود لدينا صنفين

Customer لتخزين بيانات عميل

CustomersCollection لإدارة بيانات العملاء (اضافة حذف وتعديل ...)

نقوم بتخزين بيانات العملاء في DataTable

هذا الكود بعد التعديل ولكن أظن أن لدي مشكلة في طريقة استخدام الواجهة interface

فأنا لا أريد الشخص اللذي يستخدم الصنف أن يتعامل مع CustID وذلك لتجنب التكرار وادخال الأرقام المكررة أتمنى المساعدة من أصحاب الخبرة :)

الكود طويل ولكنه سهل ويحتوي على التعليقات

Namespace Customers

    'An Interface to Set the Customer ID
    Public Interface ICustomer
        Sub Set_CustID(ByVal CustID As Integer)
    End Interface


    Public Interface ICustomersCollection
        'propertys
        ReadOnly Property Items() As Customer()
        ReadOnly Property Items(ByVal RowFilter As String) As Customer()
        ReadOnly Property Item(ByVal indx As Integer) As Customer
        ReadOnly Property Count() As Integer
        ReadOnly Property DataSource() As DataTable
        ReadOnly Property FilteredDataSource() As DataTable
        ReadOnly Property IndexOf(ByVal Cust As Customer) As Integer


        'Public Methods
        Sub Add(ByVal Cust As Customer)
        Sub Remove(ByVal Cust As Customer)
        Sub RemoveAt(ByVal indx As Integer)
        Function NewCustomer() As Customer

    End Interface




    Public Class Customer
        Implements ICustomer


        Private _CustID As Integer
        Public ReadOnly Property CustID() As Integer
            Get
                Return _CustID
            End Get
        End Property

        'to Set
        Private Sub Set_CustID(ByVal CustID As Integer) Implements ICustomer.Set_CustID
            _CustID = CustID
        End Sub

        Private _CustName As String
        Public Property CustName() As String
            Get
                Return _CustName
            End Get
            Set(ByVal Value As String)
                If Value = Nothing Then Throw New NullReferenceException("Name Can't be Null")
                _CustName = Value
            End Set
        End Property

        Private _CustPhone As String
        Public Property CustPhone() As String
            Get
                Return _CustPhone
            End Get
            Set(ByVal Value As String)
                If Value = Nothing Then Throw New NullReferenceException("Phone Can't be Null")
                _CustPhone = Value
            End Set
        End Property

        Private _CustNotes As String
        Public Property CustNotes() As String
            Get
                Return _CustNotes
            End Get
            Set(ByVal Value As String)
                _CustNotes = Value
            End Set
        End Property

        'remove the Customer Object from the Collection
        Public Sub Remove()
            Dim CI As ICustomersCollection
            CI.Remove(Me)
        End Sub

        'the index of the Customer Object in the Collection
        Public Function Index() As Integer
            Dim CI As ICustomersCollection
            Return CI.IndexOf(Me)
        End Function

    End Class

    'a Class to Store and Manage Customers 
    'this Class Store the Customers in a DataTable
    Public Class CustomesCollection
        Implements ICustomersCollection

        Private CollTable As DataTable 'To Store Customers Information

        Sub New()
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'write here a Code For Getting the Data From the Real Table
            'into a DataTable (CollTable)
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'Creating the Table
            CollTable = New DataTable("Customers")
            CollTable.Columns.Add("CustID", GetType(Integer))
            CollTable.Columns.Add("CustName", GetType(String))
            CollTable.Columns.Add("CustPhone", GetType(String))
            CollTable.Columns.Add("CustNotes", GetType(String))

            'Adding a Sample Data to it 
            Dim row As DataRow = CollTable.NewRow
            row.ItemArray = New Object() {1, "Khalid", "999", ""}
            CollTable.Rows.Add(row)
            row = CollTable.NewRow
            row.ItemArray = New Object() {2, "Ahmmad", "7777", ""}
            CollTable.Rows.Add(row)
            row = CollTable.NewRow
            row.ItemArray = New Object() {3, "Omar", "2222", "Some Notes"}
            CollTable.Rows.Add(row)

        End Sub

        'returns Number of Items in the Collection 
        'by Counting the rows in CollTable
        Public ReadOnly Property Count() As Integer Implements ICustomersCollection.Count
            Get
                Return CollTable.Rows.Count
            End Get
        End Property

        'returns the index of an object in the Collection
        Public ReadOnly Property IndexOf(ByVal Cust As Customer) As Integer Implements ICustomersCollection.IndexOf
            Get
                For i As Integer = 0 To CollTable.Rows.Count - 1
                    If CollTable.Rows(i).Item("CustID") = Cust.CustID Then Return i
                Next
                Return -1
            End Get
        End Property

        'Returns a Copy of the Collection Table (CollTable)
        Public ReadOnly Property DataSource() As DataTable Implements ICustomersCollection.DataSource
            Get
                Return CollTable.Copy
            End Get
        End Property

        'returns a the Collection Table after filtering it
        Public ReadOnly Property FilteredDataSource() As DataTable Implements ICustomersCollection.FilteredDataSource
            Get
                Return CollTable
            End Get
        End Property

        'returns All Items in the Collection by Converting the rows 
        'in the CollTable To Customer Object Array
        Public ReadOnly Property Items() As Customer() Implements ICustomersCollection.Items
            Get
                Dim Coll As New ArrayList
                For Each row As DataRow In CollTable.Rows
                    Coll.Add(ConvertRowToCustomer(row))
                Next
                Return Coll.ToArray(GetType(Customer))
            End Get
        End Property

        'returns Items after filtring the Items 
        Public ReadOnly Property Items(ByVal RowFilter As String) As Customer() Implements ICustomersCollection.Items
            Get
                Dim Coll As New ArrayList
                Dim dv As DataView = CollTable.DefaultView
                dv.RowFilter = RowFilter
                If dv.Count > 0 Then
                    For i As Integer = 0 To dv.Count - 1
                        Coll.Add(ConvertRowToCustomer(dv.Item(i).Row))
                    Next
                    Return Coll.ToArray(GetType(Customer))
                End If
                Return Nothing
            End Get
        End Property

        'returns an Item by the index
        Public ReadOnly Property Item(ByVal indx As Integer) As Customer Implements ICustomersCollection.Item
            Get
                If indx >= CollTable.Rows.Count Then Throw New IndexOutOfRangeException("Can't find index " + indx.ToString() + " in the Collection")
                Return ConvertRowToCustomer(CollTable.Rows(indx))
            End Get
        End Property

        'returns a new Customer Object and Set a new Id to it
        Public Function NewCustomer() As Customer Implements ICustomersCollection.NewCustomer
            Dim Max As Integer = 0
            For Each row As DataRow In CollTable.Rows
                If row.Item("CustID") > Max Then
                    Max = row.Item("CustID")
                End If
            Next
            Dim Cust As New Customer
            Dim IC As ICustomer
            IC.Set_CustID(Max + 1)
            Return Cust
        End Function

        'Add A new Customer object to the Collection
        Public Sub Add(ByVal Cust As Customer) Implements ICustomersCollection.Add
            If Cust Is Nothing Then Throw New NullReferenceException
            If Cust.CustID = Nothing Then Throw New NullReferenceException("Cant add a Customer without and ID")
            If Items("CustID=" + Cust.CustID.ToString()).Length <> 0 Then
                '(updating) remove old Customer 
                Remove(Cust)
            End If
            CollTable.Rows.Add(ConvertCustomerToRow(Cust))
        End Sub

        'Deleting a Customer Object from Collection
        Public Sub Remove(ByVal Cust As Customer) Implements ICustomersCollection.Remove
            Dim dv As DataView = CollTable.DefaultView
            dv.RowFilter = "CustID=" + Cust.CustID.ToString()
            If dv.Count = 0 Then Throw New NullReferenceException("Object does not have a reference")
            dv.Item(0).Row.Delete()
        End Sub

        'Deleting a Customer Object from Collection at spcefice index
        Public Sub RemoveAt(ByVal indx As Integer) Implements ICustomersCollection.RemoveAt
            CollTable.Rows(0).Delete()
        End Sub

        'Converting a DataRow from CollTable To A Customer Object
        Private Function ConvertRowToCustomer(ByVal row As DataRow) As Customer
            Dim Cust As New Customer
            Dim IC As ICustomer
            IC.Set_CustID(row("CustID"))
            'Cust.Set_CustID(row.Item("CustID"))
            Cust.CustName = row.Item("CustName")
            Cust.CustPhone = row.Item("CustPhone")
            Cust.CustNotes = row.Item("CustNotes")
            Return Cust
        End Function

        'Converts a Customer Object into a DataRow
        Private Function ConvertCustomerToRow(ByVal Cust As Customer) As DataRow
            Dim row As DataRow = CollTable.NewRow()
            row.Item("CustID") = Cust.CustID
            row.Item("CustName") = Cust.CustName
            row.Item("CustPhone") = Cust.CustPhone
            row.Item("CustNotes") = Cust.CustNotes
            Return row
        End Function

        ''returns New Customer ID for Inserting 
        'Private Function NewID() As Integer
        '    Dim Max As Integer = 0
        '    For Each row As DataRow In CollTable.Rows
        '        If row.Item("CustID") > Max Then
        '            Max = row.Item("CustID")
        '        End If
        '    Next
        '    Return Max + 1
        'End Function
    End Class
End Namespace
#5

أنت تريد عدم قدرة المستخدم من تغيير الرقم الشخصي>>> لماذا تصنع واجهة interface وتجعل لها أمرا وظيفته تغيير هذا الرقم؟!!

لماذا تضع في الفئة Customer أمرا يجعلك تستطيع تغيير الرقم الشخصي؟!!

أين New في فئة Customer،، عليك أن تصنع واحدا يأخذ القيم الابتدائية.

public class Customer
public Sub New(id as Integer, name as String)
CustID=id
CustName=name
end sub

احذف الفئة ICustomer.

في CustomerCollection،، أضف أمرا وظيفته صناعة Customer جديد برقم شخصي جديد..

public class CustomerCollection
public Function CreateNewCustomer() as Customer
// search in your datasource for an ID number which has not been used
ابحث في بياناتك عن رقم شخصي غير مستعمل،،،
id = getFreeID()
customer = New Customer(id, "No Name")
return customer
end sub

أرجو أن تكون هذه مفيدة!

بالتوفيق!

تم تعديل هذه المشاركة بواسطة أبومازن في 19 ديسمبر 2005 في 11:08

#6

السلام عليكم ورحمة الله وبركاته

هل من الممكن وضع المرفق الخاص بالكود الجديد

أما بالنسبة للسؤال :

اقتباس
1- إذا كان CustID يمكن القراءة كيف يمكنني أن أضع فيه قيمة من خلال CustomersCollection Class

فعلى حد ما فهمت من السؤال فالحل منطقي أعطي لها Set

Set
_CustID = value
End Set

Name : Ahmed H. Alawady

Web Site : Alawady.info

Email : alawady_ahmed@hotmail.com

Tel : +2 012 345 6808

#7

أشكرك يا أستاذ أبو مازن على التفاعل والصبر

وأشكرك أيضا يا أخ أحمد على التجاوب

لماذا وضعت Set_CustID في Customer Class?

الجواب هو لأني أريد أن يكون للCustomersCollection Class فقط القدرة على تغيير الCustID

فتصور مع الكود التالي لشخص ما أرد استخدام Customers NameSpace الذي يحتوي على Customer,CustomersCollection

Dim CustColl as new CustomersCollection

'Add new Customer

private Sub Add()

Dim Cust as Customer 'I dont want the him to Give the CustID

Cust=CustColl.NewCustomer 'Getting a new Customer object with a free CustID

Cust.CustName="أبو مازن" 'Setting the Name

Cust.CustPhone="5454353" 'Setting the Phone

CustColl.Add(Cust)

End Sub

لو إنتقلنا الآن الى داخل CustomersCollection.NewCustomer التي استخدمتها في السطر رقم 4 من الكود

سنجد الكود التالي

 Public Function NewCustomer() As Customer Implements ICustomersCollection.NewCustomer
            Dim Max As Integer = 0
            For Each row As DataRow In CollTable.Rows
                If row.Item("CustID") > Max Then
                    Max = row.Item("CustID")
                End If
            Next
            Dim Cust as new Customer
            Cust.CustID=Max+1   '<----- وهنا تكمن المشكلة
            Return Cust
End Function

أتمنى أتصبرو علي قد تكون المشكلة بسيطة ولكنها في نظري معقدة :wacko:

CustomersClass.rar

تم تعديل هذه المشاركة بواسطة W3Developer في 19 ديسمبر 2005 في 13:56

#8
Dim CustColl as new CustomersCollection

'Add new Customer

private Sub Add()

Dim Cust as Customer 'I dont want the him to Give the CustID

Cust=CustColl.NewCustomer("W3Developer", 535353) 'Getting a new Customer object with a free CustID

CustColl.Add(Cust)

End Sub

وفي CollectionCustomer

Public Function NewCustomer(n as String, phone as String) As Customer Implements ICustomersCollection.NewCustomer
           Dim Max As Integer = 0
           For Each row As DataRow In CollTable.Rows
               If row.Item("CustID") > Max Then
                   Max = row.Item("CustID")
               End If
           Next
           
           id=Max+1  
Dim Cust as new Customer(id, n, phone) هنا تحل المشكلة، من غير setID
add(cust)
           Return Cust
End Function

لاحظ أني لم أستخدم setID،،

تم تعديل هذه المشاركة بواسطة أبومازن في 19 ديسمبر 2005 في 17:12

#9

جزاك الله خير يا أستاذ أبو مازن

عندي سؤال -أن شاء الله يكون الأخير-

هل يمكنني أن أمنع أستخدام NEW عند إنشاء الصنف Customer إلا في حالة استخدام الأمر من داخل CustomersCollection

أي يكون الاعلان عن المتغير من نوع Customer بالشكل التالي عندما يكون الاعلان خارج CustomersCollection

'if the Declare out side the CustomersCollection Class
Dim Cust as Customer

أما إذا كان الاعلان داخل CustomersCollection يكون مخيرا بين الشكلين

'if the Declare in side the CustomersCollection
Dim Cust as Customer
'or
Dim Cust as New Customer

لماذا هذا السؤال؟

لأنه وبكل سهولة يستطيع المستخدم للحزمة أن يتحايل ويضع ID من عنده إذا استخدم

Dim CustColl as new CustomersCollection

'Add new Customer

private Sub Add()

Dim Cust as new Customer (22,"arab team","4342432") 'I dont want the him to Give the CustID

CustColl.Add(Cust)

End Sub

وبالتالي فلن بحتاج الى إستخدام CustomersCollecion.NewCustomer التي ترجع كائن من نوع Customer يحتوي على

CustID جديد

أتمنى أن أكون وفقت في إيصال الفكرة كنت أتوقع أن الموضوع مطروق بكثرة من قبل لأنه تعامل مع قواعد البيانات باستخدام البرمجة الشيئية

#10

1- تستطيع أن تجعل المنشئ أو الباني New له صلاحية Friend وليس public وبهذا لن يستطيع أحد أن يستخدم New إلا الفئات التي داخل الامتداد namespace الذي أنت تحدده.

2- لكنك تستطيع أن تجعل الأمر Add يتأكد من وجود id فارغ ليتم إضافته ، وإلا أطلق خطأ،،

public function add(c as Customer)
if c.id is in the database throw new IDAlreadyUsed
else add the customer in the database

وهذا من أفضل الطرق! فحتى لو كان عند المستخدم صلاحية إنشاء Customer فلن تتم إضافته إلا إذا كان عنده رقم غير محجوز!

أرجو أن يكون هذا مفيدا.

#11

السلام عليكم

بالنسبة لCustomerID لما لا تجعله معرف مفرد Guid وبهذا لن يتم تكراره

وبالنسبة للفئة CustomesCollection خليها ترث من الفئة القاعديةCollectionBase

عملت مثال يوضح الفكرة قليلا وتسطيع تطويره والتعديل فيه واضافة جميع الوظائف التى تحتاجها فى عملك قد يحتوى على اخطاء ولكنه لتوضيح الفكرة ليس الا...

Namespace Customers
    Public Interface ICostumer
        ReadOnly Property CustomerID() As String
        Property CustomerName() As String
        Property CustomerAddress() As String
        Property CustomerPhone() As String
        Property CustomerNote() As String
        Function ToString() As String

    End Interface
    Public Class CustomerClass
        Implements ICostumer
        Private _CustomerID As String
        Private _CustomerName As String
        Private _CustomerAddress As String
        Private _CustomerPhone As String
        Private _CustomerNote As String


        Public Sub New(ByVal CustomerName As String, ByVal CoustomerAddress As String, ByVal CustomerPhone As String, Optional ByVal CustomerNote As String = "")
            _CustomerID = Guid.NewGuid.ToString
            _CustomerName = CustomerName
            _CustomerAddress = CustomerAddress
            _CustomerPhone = CustomerPhone
            _CustomerNote = CustomerNote

        End Sub
        Public Property CustomerAddress() As String Implements ICostumer.CustomerAddress
            Get
                Return _CustomerAddress
            End Get
            Set(ByVal Value As String)
                _CustomerAddress = Value
            End Set
        End Property

        Public ReadOnly Property CustomerID() As String Implements ICostumer.CustomerID
            Get
                Return _CustomerID
            End Get
        End Property

        Public Property CustomerName() As String Implements ICostumer.CustomerName
            Get
                Return _CustomerName
            End Get
            Set(ByVal Value As String)
                _CustomerName = Value
            End Set
        End Property

        Public Property CustomerPhone() As String Implements ICostumer.CustomerPhone
            Get
                Return _CustomerPhone
            End Get
            Set(ByVal Value As String)
                _CustomerPhone = Value
            End Set
        End Property

        Public Overrides Function ToString() As String Implements ICostumer.ToString

        End Function

        Public Property CustomerNote() As String Implements ICostumer.CustomerNote
            Get
                Return _CustomerNote
            End Get
            Set(ByVal Value As String)
                _CustomerNote = Value
            End Set
        End Property
    End Class
    '''*************
    Public Class CustomersCollection
        Inherits System.Collections.CollectionBase
        Public Sub add(ByVal Customer As CustomerClass)
            list.Add(Customer)
        End Sub
        Public Sub Remove(ByVal Item As CustomerClass)
            list.Remove(Item)
        End Sub
        Default Public Property Item(ByVal index As Integer) As CustomerClass
            Get
                If index > Count - 1 Or index < 0 Then

                    Dim Exc As New Exception("لا يوجد عنصر بهذا الرقم.")
                    Throw Exc
                Else
                    Return CType(List.Item(index), CustomerClass)

                End If

            End Get
            Set(ByVal Value As CustomerClass)
                If index > Count - 1 Or index < 0 Then
                    Dim Exc As New Exception("لا يوجد عنصر بهذا الرقم.")
                    Throw Exc
                Else
                    List.Item(index) = Value
                End If

            End Set
        End Property
        Public Shadows ReadOnly Property Count() As Integer
            Get
                Return list.Count
            End Get

        End Property



    End Class
End Namespace

تم تعديل هذه المشاركة بواسطة SOLO.NET في 19 ديسمبر 2005 في 19:29

VB.NET and C# Comparison

http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

002.gif

=-=-=-=-=-=-=
ذو العلم يشقى فى النعيم بعلمه .:. واخو الجهالة فى الشقاوة ينعم
=-=-=-=-=-=-=
يا من بدنياه اشتغل . قد غره طول الأمل . فالموت يأتى بغتة . والقبر صندوق العمل
=-=-=-=-=-=-=
ان لم تستطع ان تجد ما تفعله فافعل ما لم تسطع ان تجده

screen_shot_2011-05-19_at_12.44.49_pm.pn

#12

أشكرك جدا يا أستاذ أبو مازن على التفاعل الجميل والسريع وبالفعل Friend حلت المشكلة ولكن بعد تحويل Customers NameSpace الى ملف Dll

وأشكر أيضا يا أخ Solo.net ولقد استفدت كثيرا من الClass الذي كتبته ولكن لدي بعض التساؤلات

1-ما هو الأفضل: استخدام DataTable لحفظ معلومات العملاء أو إستخدام CollectionBase ولماذا؟

2-اذا استخدمت الCollectionBase كيف يمكنني تحويل أمر البحث بإستخدام rowFilter الى الشكل الجديد حاليا الكود هو:

'returns Items after filtring the Items 
        Public ReadOnly Property Items(ByVal RowFilter As String) As Customer() Implements ICustomersCollection.Items
            Get
                Dim Coll As New ArrayList
                Dim dv As DataView = CollTable.DefaultView
                dv.RowFilter = RowFilter
                If dv.Count > 0 Then
                    For i As Integer = 0 To dv.Count - 1
                        Coll.Add(ConvertRowToCustomer(dv.Item(i).Row))
                    Next
                    Return Coll.ToArray(GetType(Customer))
                End If
                Return Nothing
            End Get
        End Property

وجزاكم الله خيرا وأحسن إليكم

#13

الحزمة بعد التعديل:

Namespace Customers

    Public Interface ICustomersCollection
        'propertys
        ReadOnly Property Items() As Customer()
        ReadOnly Property Items(ByVal RowFilter As String) As Customer()
        ReadOnly Property Item(ByVal indx As Integer) As Customer
        ReadOnly Property Count() As Integer
        ReadOnly Property DataSource() As DataTable
        ReadOnly Property FilteredDataSource() As DataTable
        ReadOnly Property IndexOf(ByVal Cust As Customer) As Integer


        'Public Methods
        Sub Add(ByVal Cust As Customer)
        Sub Remove(ByVal Cust As Customer)
        Sub RemoveAt(ByVal indx As Integer)
        Function NewCustomer() As Customer

    End Interface




    Public Class Customer
        Friend Sub New()

        End Sub

        Friend Sub New(ByVal ID As Integer)
            _CustID = ID
        End Sub

        Friend Sub New(ByVal ID As Integer, ByVal name As String)
            _CustID = ID
            _CustName = name
        End Sub

        Friend Sub New(ByVal ID As Integer, ByVal Name As String, ByVal Phone As String)
            _CustID = ID
            _CustName = Name
            _CustPhone = Phone
        End Sub

        Private _CustID As Integer
        Public ReadOnly Property CustID() As Integer
            Get
                Return _CustID
            End Get
        End Property

        'to Set
        Private Sub Set_CustID(ByVal CustID As Integer)
            _CustID = CustID
        End Sub

        Private _CustName As String
        Public Property CustName() As String
            Get
                Return _CustName
            End Get
            Set(ByVal Value As String)
                If Value = Nothing Then Throw New NullReferenceException("Name Can't be Null")
                _CustName = Value
            End Set
        End Property

        Private _CustPhone As String
        Public Property CustPhone() As String
            Get
                Return _CustPhone
            End Get
            Set(ByVal Value As String)
                If Value = Nothing Then Throw New NullReferenceException("Phone Can't be Null")
                _CustPhone = Value
            End Set
        End Property

        Private _CustNotes As String
        Public Property CustNotes() As String
            Get
                Return _CustNotes
            End Get
            Set(ByVal Value As String)
                _CustNotes = Value
            End Set
        End Property

        'remove the Customer Object from the Collection
        Public Sub Remove()
            Dim CI As ICustomersCollection
            CI.Remove(Me)
        End Sub

        'the index of the Customer Object in the Collection
        Public Function Index() As Integer
            Dim CI As ICustomersCollection
            Return CI.IndexOf(Me)
        End Function

    End Class

    'a Class to Store and Manage Customers 
    'this Class Store the Customers in a DataTable
    Public Class CustomesCollection
        Implements ICustomersCollection

        Private CollTable As DataTable 'To Store Customers Information

        Sub New()
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'write here a Code For Getting the Data From the Real Table
            'into a DataTable (CollTable)
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'Creating the Table
            CollTable = New DataTable("Customers")
            CollTable.Columns.Add("CustID", GetType(Integer))
            CollTable.Columns.Add("CustName", GetType(String))
            CollTable.Columns.Add("CustPhone", GetType(String))
            CollTable.Columns.Add("CustNotes", GetType(String))

            'Adding a Sample Data to it 
            Dim row As DataRow = CollTable.NewRow
            row.ItemArray = New Object() {1, "Khalid", "999", ""}
            CollTable.Rows.Add(row)
            row = CollTable.NewRow
            row.ItemArray = New Object() {2, "Ahmmad", "7777", ""}
            CollTable.Rows.Add(row)
            row = CollTable.NewRow
            row.ItemArray = New Object() {3, "Omar", "2222", "Some Notes"}
            CollTable.Rows.Add(row)

        End Sub

        'returns Number of Items in the Collection 
        'by Counting the rows in CollTable
        Public ReadOnly Property Count() As Integer Implements ICustomersCollection.Count
            Get
                Return CollTable.Rows.Count
            End Get
        End Property

        'returns the index of an object in the Collection
        Public ReadOnly Property IndexOf(ByVal Cust As Customer) As Integer Implements ICustomersCollection.IndexOf
            Get
                For i As Integer = 0 To CollTable.Rows.Count - 1
                    If CollTable.Rows(i).Item("CustID") = Cust.CustID Then Return i
                Next
                Return -1
            End Get
        End Property

        'Returns a Copy of the Collection Table (CollTable)
        Public ReadOnly Property DataSource() As DataTable Implements ICustomersCollection.DataSource
            Get
                Return CollTable.Copy
            End Get
        End Property

        'returns a the Collection Table after filtering it
        Public ReadOnly Property FilteredDataSource() As DataTable Implements ICustomersCollection.FilteredDataSource
            Get
                Return CollTable
            End Get
        End Property

        'returns All Items in the Collection by Converting the rows 
        'in the CollTable To Customer Object Array
        Public ReadOnly Property Items() As Customer() Implements ICustomersCollection.Items
            Get
                Dim Coll As New ArrayList
                For Each row As DataRow In CollTable.Rows
                    Coll.Add(ConvertRowToCustomer(row))
                Next
                Return Coll.ToArray(GetType(Customer))
            End Get
        End Property

        'returns Items after filtring the Items 
        Public ReadOnly Property Items(ByVal RowFilter As String) As Customer() Implements ICustomersCollection.Items
            Get
                Dim Coll As New ArrayList
                Dim dv As DataView = CollTable.DefaultView
                dv.RowFilter = RowFilter
                If dv.Count > 0 Then
                    For i As Integer = 0 To dv.Count - 1
                        Coll.Add(ConvertRowToCustomer(dv.Item(i).Row))
                    Next
                    Return Coll.ToArray(GetType(Customer))
                End If
                Return Nothing
            End Get
        End Property

        'returns an Item by the index
        Public ReadOnly Property Item(ByVal indx As Integer) As Customer Implements ICustomersCollection.Item
            Get
                If indx >= CollTable.Rows.Count Then Throw New IndexOutOfRangeException("Can't find index " + indx.ToString() + " in the Collection")
                Return ConvertRowToCustomer(CollTable.Rows(indx))
            End Get
        End Property

        'returns a new Customer Object and Set a new Id to it
        Public Function NewCustomer() As Customer Implements ICustomersCollection.NewCustomer
            Dim Max As Integer = 0
            For Each row As DataRow In CollTable.Rows
                If row.Item("CustID") > Max Then
                    Max = row.Item("CustID")
                End If
            Next
            Dim Cust As New Customer(Max + 1)
            Return Cust
        End Function

        'Add A new Customer object to the Collection
        Public Sub Add(ByVal Cust As Customer) Implements ICustomersCollection.Add
            If Cust Is Nothing Then Throw New NullReferenceException
            If Cust.CustID = Nothing Then Throw New NullReferenceException("Cant add a Customer without and ID Please Use NewCustomer Function To Get a New Customer Object With a free ID")
            If Not Items("CustID=" + Cust.CustID.ToString()) Is Nothing Then
                '(updating) remove old Customer 
                Remove(Cust)
            End If
            CollTable.Rows.Add(ConvertCustomerToRow(Cust))
        End Sub

        'Deleting a Customer Object from Collection
        Public Sub Remove(ByVal Cust As Customer) Implements ICustomersCollection.Remove
            Dim dv As DataView = CollTable.DefaultView
            dv.RowFilter = "CustID=" + Cust.CustID.ToString()
            If dv.Count = 0 Then Throw New NullReferenceException("Object does not have a reference")
            dv.Item(0).Row.Delete()
        End Sub

        'Deleting a Customer Object from Collection at spcefice index
        Public Sub RemoveAt(ByVal indx As Integer) Implements ICustomersCollection.RemoveAt
            CollTable.Rows(0).Delete()
        End Sub

        'Converting a DataRow from CollTable To A Customer Object
        Private Function ConvertRowToCustomer(ByVal row As DataRow) As Customer
            Dim Cust As New Customer(row.Item("CustID"))
            Cust.CustName = row.Item("CustName")
            Cust.CustPhone = row.Item("CustPhone")
            Cust.CustNotes = row.Item("CustNotes")
            Return Cust
        End Function

        'Converts a Customer Object into a DataRow
        Private Function ConvertCustomerToRow(ByVal Cust As Customer) As DataRow
            Dim row As DataRow = CollTable.NewRow()
            row.Item("CustID") = Cust.CustID
            row.Item("CustName") = Cust.CustName
            row.Item("CustPhone") = Cust.CustPhone
            row.Item("CustNotes") = Cust.CustNotes
            Return row
        End Function

    End Class
End Namespace

OOPCustomerClass.rar

UserInterfaceToTestClass.rar

#14

إلى هنا لا أستطيع إفادتك بشيء جديد،،

لكن الاختبار الأول هو استخدامك لهذه الحزمة في برنامجك.. فإن أحسست أنها وفت وكفت فهنيئا لك!

بالتوفيق!!

هذا الموضوع مغلق.

مواضيع مشابهة