Share via


Difference between Public and Shared variable

Question

Thursday, July 26, 2007 7:16 AM

I was just wondering what is the difference between Public and Shared variable. I have the following

Public avalue As Value

Protected Function A()
avalue = 2
End Function

Protected Function B()
Dim bvalue As Value
bvalue = avalue
End Function

My question is if I declare aValue as Shared then aValue retains the value of 2whearas if I declare it as Public the original value is lost. I also need to know whether it is recommended to use Share variable within the program.

<!-- / message -->

All replies (2)

Thursday, July 26, 2007 7:45 AM ✅Answered

Hi There,

This article have good explaination on Shared keyword:

http://www.developerfusion.co.uk/show/1047/5/

http://www.ondotnet.com/pub/a/dotnet/2002/11/11/singleton.html


Thursday, July 26, 2007 7:48 AM ✅Answered

Hi Rajabadha,

A public is an access modifier which means that it is the scope of this construct is public i.e. anyone can use it.

The Shared keyword is a modifier that may be applied to either a variable in a class or a method inside of a class. If you declare a variable with this modifier, that variable will only be created once no matter how many instances of the class are created. If you access this variable from any instance, or change this variable from any instance, then the value will be the same across all instances. A method declared as Shared can be called without creating an instance of that class. Think of the MessagBox.Show method. You do not have to create a MessageBox object, you just use the Show method.

consult this thread http://msdn.microsoft.com/en-us/library/ms973875.aspx

 

Good luck