[2005] Inheritance & style
This post is slightly unusual in the fact that I already have a solution to the problem, but the question is... is it the correct one? or the most appropriate one for the task at hand.
This is probably more a question of 'style' or the 'art' of programming, and I would appreciate it if people could make comments, suggestions, criticisms or offer alternatives solutions.
In the real world I have a program with a number of DataGridViews all of which are created at runtime. Some of the properties have same values etc., but there are quite a number of differences. So to eliminate as much duplicate code as possible I thought of putting ReadOnly properties in their own classes.
What follows is a very simple piece of code which shows what I am trying to accomplish.
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If radClass1.Checked Then
Dim cls As New BaseClass
TextBoxSetup(cls)
Else
Dim cls As New DerivedClass
TextBoxSetup(cls)
End If
End Sub
Private Sub TextBoxSetup(ByVal tbClass As BaseClass)
TextBox1.BackColor = tbClass.bColor
TextBox1.ForeColor = tbClass.fColor
TextBox1.Text = tbClass.mText
End Sub
End Class
Public Class BaseClass
Private m_bcolor As Color = Color.Yellow
Private m_fcolor As Color = Color.Red
Private m_text As String = "Class1"
Public Overridable ReadOnly Property bColor() As Color
Get
Return m_bcolor
End Get
End Property
Public Overridable ReadOnly Property fColor() As Color
Get
Return m_fcolor
End Get
End Property
Public Overridable ReadOnly Property mText() As String
Get
Return m_text
End Get
End Property
End Class
Public Class DerivedClass
Inherits BaseClass
Private m_bcolor As Color = Color.LimeGreen
Private m_fcolor As Color = Color.Blue
Private m_text As String = "Class2"
Public Overrides ReadOnly Property bColor() As Color
Get
Return m_bcolor
End Get
End Property
Public Overrides ReadOnly Property fColor() As Color
Get
Return m_fcolor
End Get
End Property
Public Overrides ReadOnly Property mText() As String
Get
Return m_text
End Get
End Property
End Class
Cheers