Understanding "Public Shared" Declaration

Hi,

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)?

Thanks,
Sagi
LVL 1
sagirodinAsked:
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.

recklezCommented:
Sagi,

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.
0
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,
Thanks.
0
gregoryyoungCommented:
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)
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

sagirodinAuthor Commented:
Ok,
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?

Thanks,
Sagi
0
gregoryyoungCommented:
yes you could do that ... but generally what I would do is make the parent do it instead of accessing from child -> child.

ex:

Parent.GetChild("SomeNameForMyOtherControl")

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

Parent.SetSecurityPassed(Username)

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.
0
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?

Thanks,
Sagi
0
gregoryyoungCommented:
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.
0
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?
0
gregoryyoungCommented:
in your debugger ... look at the type of Me.Parent, what is it?
0
sagirodinAuthor Commented:
It's a control of course
0
gregoryyoungCommented:
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?
0
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?
0
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?
0
gregoryyoungCommented:

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
0

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!
0
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
ASP.NET

From novice to tech pro — start learning today.