Is shared safe in this example?

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)

Example below - a simple routine that fills a dropdownlist or list. Would it be safe being accessed at multiple times by
the APSX pages? I think so, but I need an a more knowledgeable opinion.

Public Class MyUtils
Public Shared Function FillList(ByVal objList As Object, 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
End Class

Usage...
FillList(cmbListOfNames, AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.MANAGEMENT"))
 
C

Cor Ligthert

Hi Stevie,

I never used it and I do not know what you want to archive, with this shared
function. However maybe I am wrong, I do not direct see a reason to make it
shared so maybe you can tell us that.

Some things in your code.
In my opinion you can pass that List as Listbox and than you are not using
late binding, now you use more resources than needed. (And because it is
even in the read loop I think really a lot of time). When you want to do as
well a combobox as a listbox, than you can do it this way, however than you
should cast it to a listcontrol when filling it in my opinion.

To prevent this small things the next time, set option strict on in top of
your program, than you are warned on this kind of things.

Disposing of the command has no sence, while I think in this the connection
can better be disposed because of the connection.pooling as Angel always
says.

Just some thoughts,

:)

Cor
Public Class MyUtils
Public Shared Function FillList(ByVal objList As Object, ByVal sSql As
String, ByVal sConnection As String) As
 
S

Stevie_mac

Hi Cor & thanks for taking the time to answer.

The reason for Object rather than ListBox is that the function will accept either DropDown or ListBox. This isn't the
issue - I know I could (perhaps should) use overloading, but speed is by no means an issue here.

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

These would be used in many by different functions in many different Classes / Web pages etc. Like calls to a common
library.

Imports MyUtils
Class SomeWebPage
.. . .
.. . .

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If IsPostBack = False Then
FillList(cmbList1, sSqlQuery, sConStr) '<<< Function call to MyUtills.FillList
SortList(cmbList1,True) '<<< Function call to MyUtills.SortList
SelectListItem(cmbList1,sUserInput) '<<< Function call to MyUtills.SelectListItem
End If
End Sub
.. . .
.. . .
End Class


My question was Is It Safe - Since many different calls will be made to the same 'shared' functions - is there a problem
in coding this way
 
C

Cor Ligthert

Hi Stevie,

I see why you make this class, however why "shared" and why not just normal
and use it as

dim uttilities as new uttilityclass

And that late binding is to overcome I think with
directcast(objList, Listcontrol).Items.Add(Li)

However just my thought

Cor
 
S

Stevie_mac

Yeah, that's what I've done until I know better.

The only reason I used shared was so that I didn't have to 'Dim' objects of MyUtils - just more convenient ( and tidier
code ) to call functions directly with a single 'Imports' directive.
 
S

Stevie_mac

Cor - some info, mikeb in dotnet.languages.vb said...

<quote>
As long as the shared functions do not modify shared data, then having
multiple ASPX pages use the functions concurrently should pose no problem.

If they do access shared data (and it's going to be modified), then
you'd need to protect access to the shared data using a Monitor object
(the SyncLock statement in VB.NET or the lock keyword in C#).
</quote>

This is the answer i was seeking. Just though I'd let you know. Thanks for your time - Stevie_mac.
 

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

Top