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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Friend vs Public keyword

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
US
From what I understand, if you dim a variable as Public, other users using the same web app will have access to the values of those variables. In most cases that would be undesirable.

If I use the Friend keyword to dim a variable, will that variable be global to everything in the project for a given user, but not global to other users having their own instance of the project?

What I want is a variable that will be global to only one instance of a project and not another. Each instance of the project should have its own set of global variables.

Does that make sense?

I've read Microsoft's concepts on this and I think that is what I understand. Am I correct?

 
Let's say you create a Class Library project that contains some useful classes that you want to reuse in more than one application. Now, I'm building a web application and I want to use some of the classes from your project by referencing your dll.

Anything you delcare as Public, I will have access to. Anyting your declare as Friend, only the classes in your project (your assembly) will be able to see. Sometimes you want things to be public to the other classes in your project, but not avaiable to outside users.

In C#, this is called internal, which to me really makes it clear what the meaning is.

Hope that helps.

Greetings,
Dragonwell
 
I think so. But what I'm refering to is one application that is being used by several users, each with their own instance of the app -- like on the internet.

I want global variables that are only "seen" by the instance of the application that they are in.

I think that's what Friend does, am I right?

Also, if I dim a variable Public, all the instances of the application will have visibility to that variable, correct?
 
Hi dpdoug

No, dimming a variable as public does not make it available across instances of the application. Friend likewise does not restrict. These restrict access to the variable by other bits of code within your appplication.

No variables exist for all users of an ASP.NET application excepting Application variables. When you do the following (sorry if VB syntax slightly out)
Code:
Public Dim name As String
name = "Rob"
The variable name is created only within the scope of the request. If two users requested the same page containing the above code at exactly the same time, each would have an instance of the variable in memory specific to their request. The access modifier (e.g. Public, Private, Friend, etc) has no effect on teh sharing of variables across users.

Only the application object does this. If one users request changes the value of the application variable it would change for all users. This is the only object which does this.

Dragonwell's explanation is correct. The access modifiers only effect the access to variables by code not users. The first sentence in the link you provided above confirms this
The accessibility of a declared element is the ability to use it, that is, the permission for code to read it or write to it.

Hope this clears this up a bit for you?

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Yes. That is very clear. The reason for the confusion was that someone on this board said that Public variables were accessable by other users (besides someone on another site).

I just don't want to make the mistake and use them in my app if they are accessable since it would compromise the security of my app if they were.

I hope others see this since they hold that Public variables are visible accross instances of the application. And I've read this in several places. That's why the doubt.

Thanks for the advice.
 
In ASP.NET there is (99% of the time) only one instance of the application running, regardless of how many user sessions exists. Just one program, serving up requests from to one or more clients.

Maybe you are thinking of Shared variables? A Shared (static in C#) member is sort of like a "global" variable... maybe read up on those to get a better understanding.



Greetings,
Dragonwell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top