sagirodin
asked on
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
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
ASKER
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.
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.
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)
ASKER
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("TheI DofTHEothe rASCXcontr ol")
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
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("TheI
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
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("SomeNameF orMyOtherC ontrol")
or for often done ones I may put methods on my parent to handle it.
Parent.SetSecurityPassed(U sername)
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.
ex:
Parent.GetChild("SomeNameF
or for often done ones I may put methods on my parent to handle it.
Parent.SetSecurityPassed(U
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.
ASKER
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
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
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.
dim Parent as WhateverParentIsCalled = DirectCast(Me.Parent, WhateverParentIsCalled)
//now you can access things.
ASKER
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?
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?
ASKER
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.G etType()) what does it print?
ASKER
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("T heIDofTHEo therASCXco ntrol")
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?
dim prntControl as control = Me.Parent
dim brotherAscx as control = prntControl.FindControl("T
Works? I mean here also the TheIDofTHEotherASCXcontrol
ASKER
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.Paren t, 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 .HtmlTable Cell
returnTdAlbum = td_album
Return returnTdAlbum()
End Function
Though Now it just gives me this error:
System.StackOverflowExcept ion: Exception of type System.StackOverflowExcept ion was thrown.
When I'm trying to use this td via the call of the function that should return it. What to do?
Dim theparent As picdev = DirectCast(Me.Parent.Paren
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
returnTdAlbum = td_album
Return returnTdAlbum()
End Function
Though Now it just gives me this error:
System.StackOverflowExcept
When I'm trying to use this td via the call of the function that should return it. What to do?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Heh... Night mistake :)
Anyway you're great! Helped me alot. Will be asking you more solutions soon enough!
Anyway you're great! Helped me alot. Will be asking you more solutions soon enough!
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.