Hi,
I am looking for advice on how best to approach a particular programming
situation using VB 2005 Express.
Here is an example of what I am trying to do and how I have solved it.
Although it works, I'm not happy with my solution so I would welcome any
suggestions as to how I could better approach it.
I have two strongly typed DataTables
OrdersDataTable
QuotesDataTable
Both tables have a number of fields in common and I want to create a
sub/function that will take either an OrdersRow or a QuotesRow as an
argument without having or needing prior knowledge of which type is being
passed to it.
This is the solution I have come up with.
Public Class DataRowEx
Public Enum DataTypeEnum
Order
Quote
End Enum
Private m_DataOrderRow As DataSet.OrdersRow
Private m_DataQuoteRow As DataSet.QuotesRow
Private m_DataType As DataTypeEnum
Public Sub New(ByVal DataRowView As System.Data.DataRowView)
If TypeOf DataRowView.Row Is DataSet.OrdersRow Then
m_DataOrderRow = DirectCast(DataRowView.Row, _
DataSet.OrdersRow)
m_DataType = DataTypeEnum.Order
Exit Sub
End If
If TypeOf DataRowView.Row Is DataSet.QuotesRow Then
m_DataQuoteRow = DirectCast(DataRowView.Row, _
DataSet.QuotesRow)
m_DataType = DataTypeEnum.Quote
Exit Sub
End If
End Sub
' Expose fields that are common to both datatables
Public Function OrderID() As Int32
If m_DataType = DataTypeEnum.Order Then
Return m_DataOrderRow.OrderID
end if
Return m_DataQuoteRow.OrderID
End Function
Public Function _Date() As Date
If m_DataType = DataTypeEnum.Order Then
Return m_DataOrderRow._Date
end if
Return m_DataQuoteRow._Date
End Function
'...Lots more fields
end Class
' This function can work with either a QuoteRow or an OrderRow
Public Sub PrintDetails(ByVal row As DataRowEx)
Debug.Print(row.OrderID.ToString)
Debug.Print(row._Date.ToShortDateString)
' Do stuff with rest of fields
End Sub
As I said any comments are most welcome.
Kind regards,
Martin Horn