I have two Parser classes that inherit from a base class, BaseParser. I want to use either class as a parameter in another Monitor class. The Parser classes, CS600 and TCH600, both have two properties, RawDataList and SummaryDataList. The CS600 class’s RawDataList returns a List(of CS600Data); the TCH600 RawDataList returns a List(of TCH600Data). The SummaryDataList returns similar classes in each Parser class. CS600Data and TCH600Data derive from a base class, BaseData.
BaseParser also has RawDataList (List(of BaseData)) and SummaryDataList (List(of BaseSummaryData))
The Monitor class has a private field, _thisParser which can be either of the two concrete Parsers above. I want to be able to call and use RawDataList and SummaryDataList of _thisParser within Monitor class, but when I construct the concrete Parser classes, Visual Studio notes that the RawDataList property of CS600 cannot override the RawDataList property of the BaseParser because they differ in their return types.
I thought that since CS600Data derived from BaseData (but also adds some new properties of its own) that I could use CS600Data wherever I use BaseData. What am I misunderstanding? How can I correctly construct these classes?
Public MustInherit Class BaseParser
Protected _rawDataList As List(Of RawGasData.BaseData)
Public MustOverride ReadOnly Property RawDataList() As List(Of RawGasData.BaseData)
Protected _summaryDataList As List(Of SummaryGasData.BaseSummaryData)
Public MustOverride ReadOnly Property SummaryDataList() As List(Of SummaryGasData.BaseSummaryData)
Public Event GasDataCreated(ByVal sender As Object, ByVal e As EventArgs)
Public Sub New()
End Sub
Public Sub New(ByVal dataFilePath As String, ByVal unit As String)
Load(dataFilePath, unit)
End Sub
Public Sub Load(ByVal dataFilePath As String, ByVal unit As String)
Dim contents = My.Computer.FileSystem.ReadAllText(dataFilePath)
ParseRawData(contents)
GenerateSummaryData(contents, unit)
RaiseEvent GasDataCreated(Me, EventArgs.Empty)
End Sub
Protected MustOverride Sub ParseRawData(ByVal fileContents As String)
Protected MustOverride Sub GenerateSummaryData(ByVal fileContents As String, ByVal unit As String)
End Class
Public Class CS600Parser
Inherits BaseParser
Shadows _rawDataList As List(Of RawGasData.CS600Data)
'Cannot override base class' RawDataList - differ by their return types
Public Overrides ReadOnly Property RawDataList() As List(Of RawGasData.CS600Data)
Get
Return _rawDataList
End Get
End Property
Shadows _summaryDataList As List(Of SummaryGasData.CS600SummaryData)
'Cannot override base class' SummaryDataList - differ by their return types
Public Overrides ReadOnly Property SummaryDataList() As List(Of SummaryGasData.CS600SummaryData)
Get
Return _summaryDataList
End Get
End Property
Protected Overrides Sub ParseRawData(ByVal fileContents As String)
Dim data = From d In fileContents.Trim.Split(CChar(vbCrLf)) _
Let fields = d.Split(","c).Select(Function(s) s.Trim) _
Select New RawGasData.CS600Data With { _
.SampleNumber = fields(0), _
.UserID = fields(1), _
.CarbonValue = CDbl(fields(2)), _
.SulfurValue = CDbl(fields(3))}
_rawDataList = data.ToList
End Sub
Protected Overrides Sub GenerateSummaryData(ByVal fileContents As String, ByVal unit As String)
Dim groupedData = From d In _rawDataList _
Group By d.SampleNumber Into Group _
Select New SummaryGasData.CS600SummaryData With { _
.DataTimeStamp = Now, _
.SampleNumber = SampleNumber, _
.UserID = Group.First.UserID, _
.CarbonAverage = Group.Select(Function(s) s.CarbonValue).Average, _
.CarbonUnit = unit, _
.SulfurAverage = Group.Select(Function(s) s.SulfurValue).Average, _
.SulfurUnit = unit}
_summaryDataList = groupedData.ToList
End Sub
End Class
Public Class TCHParser
Inherits BaseParser
Shadows _rawDataList As List(Of RawGasData.TCH600Data)
'Cannot override base class' RawDataList - differ by their return types
Public Overrides ReadOnly Property RawDataList() As List(Of RawGasData.TCH600Data)
Get
Return _rawDataList
End Get
End Property
Shadows _summaryDataList As List(Of SummaryGasData.TCH600SummaryData)
'Cannot override base class' SummaryDataList - differ by their return types
Public Overrides ReadOnly Property SummaryDataList() As List(Of SummaryGasData.TCH600SummaryData)
Get
Return _summaryDataList
End Get
End Property
Protected Overrides Sub ParseRawData(ByVal fileContents As String)
Dim data = From d In fileContents.Trim.Split(CChar(vbCrLf)) _
Let fields = d.Split(","c).Select(Function(s) s.Trim) _
Select New RawGasData.TCH600Data With { _
.SampleNumber = fields(0), _
.UserID = fields(1), _
.OxygenValue = CDbl(fields(2)), _
.NitrogenValue = CDbl(fields(3)), _
.HydrogenValue = CDbl(fields(4)), _
.MassUsed = CDbl(fields(5))}
_rawDataList = data.ToList
End Sub
Protected Overrides Sub GenerateSummaryData(ByVal fileContents As String, ByVal unit As String)
Dim groupedData = From d In _rawDataList _
Group By d.SampleNumber Into Group _
Select New SummaryGasData.TCH600SummaryData With { _
.DataTimeStamp = Now, _
.SampleNumber = SampleNumber, _
.UserID = Group.First.UserID, _
.OxygenAverage = Group.Select(Function(s) s.OxygenValue).Average, _
.OxygenUnit = unit, _
.NitrogenAverage = Group.Select(Function(s) s.NitrogenValue).Average, _
.NitrogenUnit = unit, _
.HydrogenAverage = Group.Select(Function(s) s.HydrogenValue).Average, _
.HydrogenUnit = unit}
_summaryDataList = groupedData.ToList
End Sub
End Class
Public MustInherit Class BaseData
Implements IGasData
Private _massUsed As Double
Public Property MassUsed() As Double Implements IGasData.MassUsed
Get
Return _massUsed
End Get
Set(ByVal value As Double)
_massUsed = value
End Set
End Property
Private _sampleNumber As String
Public Property SampleNumber() As String Implements IGasData.SampleNumber
Get
Return _sampleNumber
End Get
Set(ByVal value As String)
_sampleNumber = value
End Set
End Property
Private _userID As String
Public Property UserID() As String Implements IGasData.UserID
Get
Return _userID
End Get
Set(ByVal value As String)
_userID = value
End Set
End Property
End Class
Public Class CS600Data
Inherits BaseData
Private _carbonValue As Double
Public Property CarbonValue() As Double
Get
Return _carbonValue
End Get
Set(ByVal value As Double)
_carbonValue = value
End Set
End Property
Private _sulfurValue As Double
Public Property SulfurValue() As Double
Get
Return _sulfurValue
End Get
Set(ByVal value As Double)
_sulfurValue = value
End Set
End Property
End Class
Public Class TCH600Data
Inherits BaseData
Private _oxygenValue As Double
Public Property OxygenValue() As Double
Get
Return _oxygenValue
End Get
Set(ByVal value As Double)
_oxygenValue = value
End Set
End Property
Private _nitrogenValue As Double
Public Property NitrogenValue() As Double
Get
Return _nitrogenValue
End Get
Set(ByVal value As Double)
_nitrogenValue = value
End Set
End Property
Private _hydrogenValue As Double
Public Property HydrogenValue() As Double
Get
Return _hydrogenValue
End Get
Set(ByVal value As Double)
_hydrogenValue = value
End Set
End Property
End Class
Your overridden list properties, such as
RawDataList, should be the type of the abstract base class (or better yetIGasData), and not the derived class for polymorphism to work. If you find that making the lists of typeBaseDataorIGasDatais causing problems, you might want to check if your design adheres to the Liskov Substitution Principle.A simplified illustration which compiles and runs just fine (VB.Net 2010):
TCHParserworks just fine overriding theRawDataListproperty. Also, I’m not sure why the abstract base class had aProtected _rawDataListfield. Remove it and you no longer have to worry about shadowing that field in derived types.