S
Stevie_mac
Hi all, quick question - I need a bit of clarity.
If I have a public class with only Public Shared functions in it, in an APS.NET web app (for common procedures) is it
safe (since multiple web pages, can at any time access the code within)
The reason for a Shared class like this is that, it would contain all of the useful functions we put together over time
like FillList, SelectListItem, FindLastItemInList, EmptyList, SortList etc etc etc
Example below - a simple routine that fills a dropdownlist. Would it be safe being accessed at multiple times by
the APSX pages? Forget the fact the code might not be perfect - only that I would like to arrange my code this way.
I suspect there may be caveats - but I need a more knowledgeable opinion.
Public Class MyUtils
Public Shared Function FillList(ByVal objList As DropDownList, ByVal sSql As String, ByVal sConnection As String) As
Boolean
Dim dr As OleDb.OleDbDataReader, Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
Try
cn.Open()
cmd = New OleDb.OleDbCommand(sSql, cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
End While
Catch ex As Exception
Stop
Return False
Finally
cmd.Dispose()
If dr Is Nothing = False Then dr.Close()
cn.Close()
End Try
Return True
End Function
Public Function EmptyList(...)
...
End Function
...
...
'Other Public Shared Functions...
...
...
End Class
Usage...
FillList(cmbListOfNames, AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.MANAGEMENT"))
If I have a public class with only Public Shared functions in it, in an APS.NET web app (for common procedures) is it
safe (since multiple web pages, can at any time access the code within)
The reason for a Shared class like this is that, it would contain all of the useful functions we put together over time
like FillList, SelectListItem, FindLastItemInList, EmptyList, SortList etc etc etc
Example below - a simple routine that fills a dropdownlist. Would it be safe being accessed at multiple times by
the APSX pages? Forget the fact the code might not be perfect - only that I would like to arrange my code this way.
I suspect there may be caveats - but I need a more knowledgeable opinion.
Public Class MyUtils
Public Shared Function FillList(ByVal objList As DropDownList, ByVal sSql As String, ByVal sConnection As String) As
Boolean
Dim dr As OleDb.OleDbDataReader, Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
Try
cn.Open()
cmd = New OleDb.OleDbCommand(sSql, cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
End While
Catch ex As Exception
Stop
Return False
Finally
cmd.Dispose()
If dr Is Nothing = False Then dr.Close()
cn.Close()
End Try
Return True
End Function
Public Function EmptyList(...)
...
End Function
...
...
'Other Public Shared Functions...
...
...
End Class
Usage...
FillList(cmbListOfNames, AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.MANAGEMENT"))