Understanding "Public Shared" Declaration


I'm trying to understand the "Public Shared" Declaration, because I think I'm having trouble on my website because I've used it alot.
If I declare some object, lets say some String on some User Class I've made as "Public Shared" and one user changes it's value or sets it to null. Will all users that are surfing the website and trying to use that object, get Null (or any other value setted for this object by the previous user)?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


When you declare a Shared variable in a class that variable will be shared among all instances
of that class.

The answer to your questions is YES.
sagirodinAuthor Commented:
Ok, then lets say I have a  ascx - class that handles the user status and shows a message which says whether the user is logged in or not. And I'm doing a login but not from within the ascx but from the aspx file that the ascx is part of it (inserted to it via page.loadcontrol).
Now after the login in the aspx file is successful I wanna change the status inside the ascx file. So I used to make a public shared sub which just changes the status of the ascx to Logged-In. Now if I don't use that (cause it should be shared among all users) how can I change the status right after I've logged in inside the aspx?

Hope it's understood,
there are many ways to solve this. The simplest would be to have the .ascx control access its parent. The parent could even have an event on it which it listened to. You can access your parent through Control.Parent (you will need to downcast appropriately). There are then many ways you can handle things (i.e. put a property "IsLoggedIn" on parent and have the child control access it etc etc)
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

sagirodinAuthor Commented:
and if I want to access it's sibling ascx? Like the ascx is inside an aspx which has another ascx in it?
Can I access the parent and then access the parent's child via
dim prntAspx as control = Me.Parent
dim brotherAscx as control = prntAspx.FindControl("TheIDofTHEotherASCXcontrol")

and then read it and do changes on it? And it'll show me it's current state for this user only??

is that right code and approach for this manner?

yes you could do that ... but generally what I would do is make the parent do it instead of accessing from child -> child.



or for often done ones I may put methods on my parent to handle it.


where parent would then orchestrate what to do when Security is passed (as opposed to the child). The reason I prefer this is that it offers lower coupling and less risk when dealing with future changes as opposed to direct child->child communications.
sagirodinAuthor Commented:
Ok, the first method (what I wrote) is working great. I'm using FindControl.
Though when I tried to build some function that would return me a HtmlTableCell from the parent.
And then I tried to use it on the child, as you wrote, it didn't work. On the child I wrote:

dim theTDfromParent as control = Parent.GetTheHtmlFunction()

It just won't let me compile saying "GetTheHtmlFunction is not a member of system.web.ui.control class.

So how can I use this one? If I want to play some sub or use some function from the parent's class?

you have to add it to the parent and appropriately cast the parent ...

dim Parent as WhateverParentIsCalled = DirectCast(Me.Parent, WhateverParentIsCalled)
//now you can access things.
sagirodinAuthor Commented:
ok. I have this control, which parent's type is called picdev . Now this is what I did:

dim theparent as picdev = DirectCast(Me.Parent, picdev)

And It gave me back an error: Specified Cast is not valid, on that line. Also Without using DirectCast it also wont work. dim theparent as picdev = Me.Parent , same error.

Why is that?
in your debugger ... look at the type of Me.Parent, what is it?
sagirodinAuthor Commented:
It's a control of course
yes but what is the actual type (its actually something that derives from control) ... if you say Response.Write(Me.Parent.GetType()) what does it print?
sagirodinAuthor Commented:
I see your point. It's a htmlTableCell. Though If so then howcom :
dim prntControl as control = Me.Parent
dim brotherAscx as control = prntControl.FindControl("TheIDofTHEotherASCXcontrol")

Works? I mean here also the TheIDofTHEotherASCXcontrol is not inside parent. Though it does find it and give's it's values and inner controls. So How do I make your idea work so I can use the methods and subs of the parent ascx control?
sagirodinAuthor Commented:
Ok I found out that Me.Parent.Parent is the ascx parent that I was Looking for so I use:

Dim theparent As picdev = DirectCast(Me.Parent.Parent, picdev)
Dim tdOnParent as htmlTableCell = theparent.returnTdAlbum()

When returnTdAlbum() is a function on the parent ascx (which has called the child that we're in):
Function returnTdAlbum() As System.Web.UI.HtmlControls.HtmlTableCell
        returnTdAlbum = td_album
        Return returnTdAlbum()
End Function

Though Now it just gives me this error:
System.StackOverflowException: Exception of type System.StackOverflowException was thrown.

When I'm trying to use this td via the call of the function that should return it. What to do?

Function returnTdAlbum() As System.Web.UI.HtmlControls.HtmlTableCell
        returnTdAlbum = td_album
        Return returnTdAlbum()
End Function

you call the function again recursively ...         Return returnTdAlbum() and infinitely do this so the stack overflows ... try ...

Function returnTdAlbum() As System.Web.UI.HtmlControls.HtmlTableCell
        return td_album
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sagirodinAuthor Commented:
Heh... Night mistake :)
Anyway you're great! Helped me alot. Will be asking you more solutions soon enough!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.