Public function scope in modules

C

Chris B.

I'm getting some strange results in an asp.net web application that has
recently gone live.

In the header of almost all the pages, the full name of the currently logged
in user is displayed. On occasion, this changes to the name of some other
user currently on the site.

Needless to say, some find this disconcerting.

The routines that get and display the name are incredibly straight forward
and I am at a loss as to how it could happen at all. The only thing I can
think of is that public functions in modules are in the application scope.
Can someone confirm this? I can't seem to find a definitive answer anywhere.

Here's a quick sketch of how the code is currently working.

There is a file, called "header.ascx" that is included at the top of every
aspx page. In this file on the Page_Load is this code:

oUser = GetUserObject()
If Not (oUser Is Nothing) Then
If IsLen(oUser.FullName) Then
lblLogIn.Text = oUser.FullName & logged in"
End If
End If

The function "GetUserObject" returns another object, clsUser. It is equally
simple and declared publicly in a module:

Public Function GetUserObject() As clsUser
Dim oUser As New clsUser
Dim oCookie As HttpCookie
Dim lngUserID As Long

oCookie = HttpContext.Current.Request.Cookies("UserID")

If Not oCookie Is Nothing Then
lngUserID = oCookie.Value

If lngUserID > 0 Then
If oUser.LoadObjbyUserID(lngUserID) Then
'Log?
End If
Else
oUser = Nothing
End If
End If

Return oUser

End Function

Any help or feedback would be appreciated.
 
T

Tom.PesterDELETETHISSS

My first thought is that the browser is caching old pages.

There are 3 levels on which caching happens
- browser
- proxy
- server

Are you sure there isnt some form of caching happening? MAybe you cache the
page on the server or send a cache-contorl header to the client.

A definite way to find out if caching is the problem is to isntruct the person
who experience the problem to hit "CTRL-F5" on internet explorer.
This will request the page and bust through all caching mechanisms. If suddenly
he sees his own name thjat meant there was some cahing going on.

If your GetUserObject function does whats its supposed to do this is probably
the reason.

I hope it helps...

Cheers,
Tom Pester
 
K

Kevin Spencer

Using Modules is generally a very bad idea. It breaks the concept of
encapsulation, which is one of the cornerstones of object-oriented
programming. VB.Net LOOKS like VB, but it certainly is not.

A Module is a static class in which everything in the Module is both static
(Shared), meaning only one instance, and everything in it is global to the
entire DLL it resides in. Furthermore, in their efforts to accomodate VB
developers, Microsoft deemed that one need not even reference the class name
of the Module in order to reference its methods and properties.

I would suggest changing the Module to a class, and avoid using anything
that is static (Shared) until you understand the nature of static (shared)
methods and properties fully. They are not thread-safe, and easy to abuse.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 
T

Tom.PesterDELETETHISSS

Oh Dear. Yes pls stay away from modules! And use option strict and practice
safe coding.

Cheers,
Tom Pester
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top