Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public vs Public Shared 2

Status
Not open for further replies.

vituja123

Programmer
Jun 16, 2005
110
0
0
US
Hi All,

I'm a little confused about the use of declaring Public vs Public Shared in a class. I want each user to have their own instance of that variable. But what does Public Shared do? If it is set by one user, then all other users will see that updated value?

-------------------------------------------
Public Class CUAuctionLocalClass
Public Shared ADMINID As String
Public Shared EMPLID As String
...
ADMINID="TEST"
End Class

So does this mean all instances will see ADMINID as TEST?
 
Yes. Shared/static variables mean that there is one instance of the variable per type. If user A sets the value to 5, user B sets the value to 7, then user A reads the variable, he will see "7" because of B's changes.

If you want each user to have their own instance of the variable don't use shared.
 
Thanks BoulderBum.

I was declaring my variables incorrectly and the latest person logged in always had their ADMINID set to them.

Thanks again.
 
Hmm,

Just one more question around the same issue.

In my class, i now declare my global vars as

Public Class CUAuctionLocalClass
Public ADMINID As String
Public EMPLID As String
...
ADMINID="TEST"
End Class

Then on other codebehind pages I do:
Private Sub Page_Load()
Dim LocalClass As CUAuctionLocalClass
label1.text = localclass.ADMINID
end sub

When I stop and check the ADMINID, it has a value of nothing. It seems that the value is only visible in the class it was declared. Am I missing something?
 
Create a property for each setting you want, Ex:"
Code:
    Public ReadOnly Property AdminID() As String
        Get
            Return "TEST"
        End Get
    End Property

Jim
 
Thanks Jim,

I'm still a nubie with asp.net. I've never created properties before. I took your example, modified it and it worked.

I'm confused why properties work but I can user PUBLIC to declare variables in my class then have the values visible in other parts of the app. Just curious...
 
PUBLIC will make the variable pulic to all subs and functions in the class, not the app. You can also use session variables.
 
Thanks again for the info Jim. I'd rather not use Session vars.
 
no reason not too.. unless you are storing large objects or very many of them.
 
Actually "Protected" would make the field visible only to its class, "Public" would indeed expose the field to other classes.

The problem must be in the way you're initializing the variable. Try:

Code:
Public ADMINID As String = "TEST"

The danger with that, however, is that another class can modify the value of ADMINID with no restriction, thus spoiling encapsulation. A better solution would be to expose the string as a read-only property like what jbenson showed, perhaps adding a ReadOnly modifier to the declaration so it can only be set during instantiation, or if you don't need to vary the value of the variable during instantiation, an even better performing (and just as safe) way to declare the variable would be to use constants:

Code:
Public Const ADMINID As String = "TEST"
 
Hi BoulderBum,

I was doing this:

Public Class CUAuctionLocalClass
Public ADMINID As String
Public EMPLID As String
...
Sub GetID

ADMINID = "TEST"
...
End Sub
End Class
--------------------------
Then from a code behind on a page:

Public Class Payments
Inherits System.Web.UI.Page
Dim LocalClass As New CUAuctionLocalClass

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.text= LocalClass.ADMINID
end sub
End Class
------------------------------------------
The ADMINID is set to Nothing. I'm just courious why? Only the property method mention by Jim retrieves a value.

 
You are not assigning a value to the ADMINID variable until the GetID function is called. In your secoind chunk of code you have not called LocalClass.GetID so the variable has not been set yet:
Code:
Public Class Payments
    Inherits System.Web.UI.Page
    Dim LocalClass As New CUAuctionLocalClass
 
     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        [highlight]LocalClass.GetID[/highlight]
        Label1.text= LocalClass.ADMINID
    end sub
End Class

Your method was getting a value, unfoirtunatly since ADMINID had not been assigned a value yet you were getting either a null or String.EmptyString value.

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top