I was given a library with generic data objects in it, Projects, Tables, Rows and Fields. In my program I have a single Project called Building, its Table 1 contains "Units", and Field 1 of Table 1 is "Unitname". So what I did was...
Public Class Unit
Private myRow As Row
Public Sub New(ByRef R as Row)
myRow = Row
End Sub
Public Function Unitname() as String
Return myRow.Fields(1)
End Function
... etc ...
End Class
That doesn't look so bad does it? But I don't have Unit objects loaded, I have Rows. So then I also have to make a another class...
Public Class Building
Private myProj as Project
...
Public Function Units() As List(Of Unit)
Dim ans as New List(Of Unit)
For Each R In myProj.Tables(1).Rows
ans.Add(new Unit(R))
Next
Return ans
' or I could use myProj.Tables(1).Rows.ConvertTo(Of Unit)
End Function
...etc...
And of course I have to make the list of Buildings so I can get to the list of Units, and the dozens of other lists and accessors, turning into thousands of lines of code who's only purpose is to make one set of objects that points to another.
It works, and I know shipping is a feature. But what I really want to do is make a Unit that looks like...
Public Class Unit
Inherits Row
...
End Class
Then I would "reverse cast" the List(Of Row) into a List(Of Unit). This would not only eliminate a lot of code, it would eliminate dangling pointers, reduce memory a whole lot, and eliminate a set-up set that might take some time. Theoretically possible, there's no difference except code, but I don't see a way to do it in VB.Net.
In Obj-C this is called swizzling (and/or extensions), and I think Java has a similar concept. I suspect that ADO.Net has to do something like this? Is there some sort of "wrap this in that" functionality I'm missing?