السلام عليكم ورحمة الله وبركاته
منذ فترة وأنا أحاول تحويل أسلوب برمجتى الى 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

