Home All Groups Group Topic Archive Search About

Advice needed on programming style

Author
3 Feb 2006 3:15 PM
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

Author
3 Feb 2006 3:45 PM
Armin Zingler
Show quote Hide quote
"Martin Horn" <mvh***@theinternet.com> schrieb
> 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
Author
3 Feb 2006 4:02 PM
Martin Horn
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.

Show quoteHide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:%23HUs9jNKGHA.3272@tk2msftngp13.phx.gbl...
> "Martin Horn" <mvh***@theinternet.com> schrieb
>> 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
>
Author
3 Feb 2006 4:59 PM
Phill W.
"Martin Horn" <mvh***@theinternet.com> wrote in message
news:KsKEf.27281$Kt5.19747@newsfe6-gui.ntli.net...

> 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.
Author
3 Feb 2006 6:09 PM
Martin Horn
> 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.