Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Understanding "Public Shared" Declaration

Posted on 2007-09-09
15
Medium Priority
?
1,296 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:sagirodin
  • 8
  • 6
15 Comments
 
LVL 4

Expert Comment

by:recklez
ID: 19857238
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
 
LVL 1

Author Comment

by:sagirodin
ID: 19857616
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 19862277
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 1

Author Comment

by:sagirodin
ID: 19862362
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 19862598
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
 
LVL 1

Author Comment

by:sagirodin
ID: 19863045
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 19863109
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
 
LVL 1

Author Comment

by:sagirodin
ID: 19863232
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 19863241
in your debugger ... look at the type of Me.Parent, what is it?
0
 
LVL 1

Author Comment

by:sagirodin
ID: 19865089
It's a control of course
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 19865115
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
 
LVL 1

Author Comment

by:sagirodin
ID: 19865172
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
 
LVL 1

Author Comment

by:sagirodin
ID: 19865190
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
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 19865200

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
 
LVL 1

Author Comment

by:sagirodin
ID: 19865278
Heh... Night mistake :)
Anyway you're great! Helped me alot. Will be asking you more solutions soon enough!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question