Repeatative Code

R

rn5a

A VB class file has the following code:

Namespace MyNS
Public Class MyClass
Private sqlConn As New SqlConnection(".....")

Public Function Categorize(ByVal Category As String) As
SqlDataReader
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader
'more code here
...............
Return sqlReader
End Function

Public Function ViewOrder(ByVal UserID As Integer) As
SqlDataReader
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader
'more code here
...............
Return sqlReader
End Function
End Class
End Namespace

Using VBC, I successfully created a DLL named MyNS.dll

This is the ASPX code:

<%@ Import Namespace="MyNS" %>
<script runat="server">
Public iUserID As Integer
Public boMyClass As MyClass
Public sqlReader As SqlDataReader

Sub Page_Load(obj As Object, ea As EventArgs)
iUserID = Request.Cookies("UserID").Value
boMyClass = New MyClass
sqlReader = boMyClass.ViewOrder(iUserID)
.............
.............
End Sub

Sub MySub1(obj As Object, ea As EventArgs)
iUserID = Request.Cookies("UserID").Value
boMyClass = New MyClass
sqlReader = boMyClass.ViewOrder(iUserID)
.............
.............
End Sub

Sub MySub2(obj As Object, ea As EventArgs)
iUserID = Request.Cookies("UserID").Value
boMyClass = New MyClass
sqlReader = boMyClass.ViewOrder(iUserID)
.............
.............
End Sub
</script>

As you can see in the ASPX code, the first 3 lines in the 3
sub-routines are exactly identical though the rest of the code in the 3
sub-routines differ vastly from each other.

What I would like to know is the way the same code snippet has been
repeated in the 3 subs - is that good programming practice? If no, can
someone suggest me how do I overcome the repeatative code?
 
T

Teemu Keiski

Hi,

can't you just centralize that to a local function on page's code-behind

Public Function GetReader() As SqlDataReader

Dim iUserID As Integer = Request.Cookies("UserID").Value
Dim boMyClass As New MyClass

Return boMyClass.ViewOrder(iUserID)
End Function

and then on page
============

Sub Page_Load(obj As Object, ea As EventArgs)
Dim sqlReader As SqlDataReader = GetReader()
.............
.............
End Sub

Sub MySub1(obj As Object, ea As EventArgs)
Dim sqlReader As SqlDataReader = GetReader()
.............
.............
End Sub

Sub MySub2(obj As Object, ea As EventArgs)
Dim sqlReader As SqlDataReader = GetReader()
.............
.............
End Sub
 

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

Similar Threads

Populate TextBox With DB Records 2
Dynamic Controls 4
SqlDataReader.Read 2
Return Value? 1
Retrieve Record Count 2
label in footertemplate of datagrid 7
Using DLL in ASPX 4
DropDown not populating? 1

Top