Lascio quindi il costruttore di default sottinteso (ossia non specifico costruttori nella classe), e creo un metodo che viene chiamato dopo.
Ci provo...Creo due classi, la prima "casella" che eredita da Label, la seconda "calendario", in cui voglio che il testo rispecchi il valore della proprietà Dati, che eredita da "casella".
Class casella Inherits Label Protected Dati As Date Sub New() BorderStyle = BorderStyle.FixedSingle End Sub Overridable Sub scriviDati(ByVal dato As String) Dati = dato End Sub End Class Class calendario Inherits casella Overrides Sub scrividati(ByVal dato As String) Dati = dato Text = Dati End Sub End ClassIl metodo scriviDati è overridable, in modo che nella classe genitrice compila solo il valore della proprietà, mentre nella classe derivata compila anche il testo con il valore della proprietà.
Ecco la procedura template:
Sub popola(Of oggetto As New)(ByVal frm As Form, ByVal dt As Object) Dim obj As Object For n = 0 To dt.Length - 1 obj = New oggetto With obj .left = 0 .top = obj.height * (n Mod dt.Length) .scriviDati(dt(n)) End With frm.Controls.Add(obj) Next End Sub...che esegue sempre la funzione scriviDati, la quale, se il tipo di oggetto fornito alla procedura template è "casella" compila soltanto il valore della proprietà Dati, mentre se il tipo di oggetto fornito alla procedura template è "calendario", fa apparire anche questo valore come testo.
Funziona!
Mi sbizzarrisco un po', traendo le conclusioni logiche più tardi...
Class casella Inherits Label Protected Dati As Date Sub New() BorderStyle = BorderStyle.FixedSingle End Sub Overridable Sub scriviDati(ByVal dato As String) Dati = dato End Sub End Class Class calendario Inherits casella Sub New() BorderStyle = BorderStyle.Fixed3D BackColor = Color.Green End Sub Overrides Sub scrividati(ByVal dato As String) Dati = dato Text = Dati End Sub End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load popola(Of calendario)(0, Me, giorniDelMese(8, 2012)) popola(Of casella)(100, Me, giorniDelMese(8, 2012)) End Sub
Sub popola(Of oggetto As New)(ByVal sinistra As Integer, ByVal frm As Form, ByVal dt As Object) Dim obj As Object For n = 0 To dt.Length - 1 obj = New oggetto With obj .left = sinistra .top = obj.height * (n Mod dt.Length) .scriviDati(dt(n)) End With frm.Controls.Add(obj) Next End SubEd ecco cosa ottengo: