كيف حل هذه المشكلة في الفئة (Class)
لدينا هذه الفئة :
Public Class Person Private mName As String Private mbirthday As Date Public Father As Person Public Property Name() As String Get Return mName End Get Set(ByVal value As String) mName = value End Set End Property Public Property Birthday() As Date Get Return mbirthday End Get Set(ByVal value As Date) mbirthday = value End Set End Property End Class
ثم لدينا هذه
Public Class PersonInFamily Inherits Person Private mSpouse As String Property Spouse() As String Get Return mSpouse End Get Set(ByVal value As String) mSpouse = value End Set End Property End Class
ومن الواضح تم اشتقاق الفئة Person داخل PersonInFamily .
ثم استخدمنا هذه الفئات كالتالي
Dim PersonObj As New PersonInFamily PersonObj.Father = New PersonInFamily PersonObj.Name = "أبن" PersonObj.Father.Name = "والد" PersonObj.Father.Father.Name = "جد"
الخطاء في الجملة الاخيرة( PersonObj.Father.Father.Name ="" ) وهو :
Object reference not set to an instance of an object.
وهذه الرسالة مقنعة، ولكن كيف الحل الأمثل إذا اردت أن أطبق الطريقة البرمجية
(LIFO (Last-In-First-Out على Class بحيث أصل إلى اسماء الأجداد عن طريق الأبناء .