Advice needed on programming style

  • Thread starter Thread starter Martin Horn
  • Start date Start date
M

Martin Horn

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
 
Martin Horn said:
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.
[...]


I see the point, but you say that "Both tables have a number of fields in
common". Maybe this is the problem? Is it possible to normalize the
database?


Armin
 
Hi Armin,

I assume that by normalize, you mean merge the tables so that the common
fields are all in one table. Due to the design of the program (which is
probably already bad), this isn't an option and the tables have to remain
seperate.

Martin.

Armin Zingler said:
Martin Horn said:
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.
[...]


I see the point, but you say that "Both tables have a number of fields in
common". Maybe this is the problem? Is it possible to normalize the
database?


Armin
 
I have two strongly typed DataTables

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.

Haven't played with Typed Datasets yet, but here's how I'd do it with
any other pair of classes.

1. Create an Interface that contains the common properties, etc.
2. Have both DataTables implement this interface.
3. Code functions to expect the Interface, not either class on their own.

as in

Public Interface ICommonFields

Function OrderID() As Integer
Function _Date() As Date
. . . more common fields ...

End Interface

Class OrdersRow
Implements ICommonFields

Public Function OrderID() as Integer _
Implements ICommonFields.OrderID
End Function
Public Function OrderDate() as Date _
Implements ICommonFields._Date
End Function
. . . more fields
End Class

Class QuotesRow
Implements ICommonFields

Public Function QuoteOrderID() as Integer _
Implements ICommonFields.OrderID
End Function
Public Function _Date() as Date _
Implements ICommonFields._Date
End Function
. . .more fields
End Class

then to use them

Private m_Row As ICommonFields

Public Sub New( ByVal DataRowView As System.Data.DataRowView)

If TypeOf DataRowView.Row Is ICommonFields Then
m_Row = DirectCast( DataRowView.Row, ICommonFields )
End If

End Sub

Public Function OrderID() As Int32
Return m_Row.OrderID
End Function

Public Function _Date() As Date
Return m_Row._Date
End Function

HTH,
Phill W.
 
1. Create an Interface that contains the common properties, etc.
2. Have both DataTables implement this interface.
3. Code functions to expect the Interface, not either class on their own.

as in

Public Interface ICommonFields

Function OrderID() As Integer
Function _Date() As Date
. . . more common fields ...

End Interface

Hi Phil,

very helpful suggestion, I shall see what I can come up with using that
approach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top