473,498 Members | 98 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Advice needed on programming style

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
Feb 3 '06 #1
4 1065
"Martin Horn" <mv****@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

Feb 3 '06 #2
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" <az*******@freenet.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Martin Horn" <mv****@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

Feb 3 '06 #3
"Martin Horn" <mv****@theinternet.com> wrote in message
news:Ks*******************@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.
Feb 3 '06 #4
> 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.
Feb 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

75
6093
by: Howard Nease | last post by:
Hello, everyone. I would appreciate any advice that someone could give me on my future career path. Here is my situation: I am a bright Junior in a very well-respected private high school, taking...
10
3069
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
1
1873
by: Philip | last post by:
Hi, I am trying to output certain nodes inside another. I have an xml template with field definitions for a form, and this includes textfields, labels, checkboxes etc plus fieldssets. I defined...
1
9582
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
8
1507
by: george.leithead | last post by:
Hi all, I'm looking for some advice on how best to achitect the following requirement. I'm basically writing a Fantasy Football (FF) Web site, and would like to have it fully OO and have it...
4
1435
by: MPA | last post by:
Hi, We are a small company with experience in client-server apps with PowerBuilder and most major databases. We have no internet experience. We are now looking into slimming our main application,...
53
4594
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
21
1532
by: Andy.I | last post by:
Hi I've just finished a collegue cource in beginning Vb.net programmeing, and was supposed to take an advanced cource in VB.net next semester learning more abour object oriented programming. But...
232
13062
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.