EriqaBana EriqaBana - 4 months ago 17
Vb.net Question

Newbie to Interface and oop concepts

I understand the concept of an interface in oop but implementing it is challenging for me. These are my classes:

Public Class Alpha
Public Property Id As Integer
Public Property StartDate as Date
Public Property System as string
Public Sub Deserialize(ByVal row As DataRow)
Me.Id = row("Id ") ...
end sub
end class

Public Class Bravo
Public Property Id As Integer
Public Property StartDate as Date
Public Property Dosage as integer
Public Sub Deserialize(ByVal row As DataRow)
Me.Id = row("Id ") ...
end sub
end class

Public Class Parent
Public Property Id As Integer
Public Property Name as String
Public Property AdmitDate as Date
Public Sub Deserialize(ByVal row As DataRow)
Me.Id = row("Id ") ...
end sub
end class

Public Class AlphaList inherits List(of Alpha)
Public Sub Deserialize(table As DataTable)
For Each objRow As DataRow In table.Rows
Dim obj As New Bravo
obj.Deserialize(objRow)
Me.Add(obj)
Next
end sub
end Class

Public Class BravoList inherits List(of Bravo)
Public Sub Deserialize(table As DataTable)
For Each objRow As DataRow In table.Rows
Dim obj As New Bravo
obj.Deserialize(objRow)
Me.Add(obj)
Next
end sub
end Class

Public Class ParentList inherits List(of Parent)
Public Sub Deserialize(table As DataTable)
For Each objRow As DataRow In table.Rows
Dim obj As New Parent
obj.Deserialize(objRow)
Me.Add(obj)
Next
end sub
end Class


Currently, I have the following two functions in Class Parent

public Sub RunA(aList as AlphaList )
For each item as Alpha in alist
item.Id ....
next
end sub
public Sub RunB(bList as BravoList )
For each item as Bravo in bList
item.Id ....
next
end sub


Basically, they have the same implementation, the only thing different is the argument - either a AlphaList or BravoList.

I want to create a function in Class Parent that takes any AlhpaList or BravoList. How would I do this? Thanks in advance!! .. I would like to extract it out all together and do some thing like (pseudocode) :

public sub Run ( anyList List of Alpha or Bravo, any list of List of Parent)

Answer

What you're asking for is not really possible. Alpha and Bravo are different types and so their StartDate properties are different properties. the fact that they have the same name doesn't change that. You could use late-binding but that would be a filthy hack.

Even if you define an interface with a StartDate property and have Alpha and Bravo implement that, it still wouldn't allow you to pass a List(Of Alpha) or a List(Of Bravo) to a parameter that expects a List(Of ISomeInterface).

If you were to have such an interface though, you could at least write a method that had a parameter of that type and then call it within your loops in the methods with the List(Of Alpha) and List(Of Bravo) parameters.