Hello all,
I am trying to learn a couple of things about VB.NET, but I have stuck to the "Shared" keyword. I am not sure what it does exactly. How does a shared property or member differ from a standard property ????
Thanks !
Printable View
Hello all,
I am trying to learn a couple of things about VB.NET, but I have stuck to the "Shared" keyword. I am not sure what it does exactly. How does a shared property or member differ from a standard property ????
Thanks !
[msdn]
Shared members are properties, procedures, and fields that are shared by all instances of a class. Some programming languages refer to such items as static members.
Shared fields and properties are useful when you have information that is part of a class, but is not specific to any one instance of a class. Normal fields and properties exist independently for each instance of a class. Changing the value of a field or property associated with any one instance does not affect the value of fields or properties of other instances of the class. On the other hand, when you change the value of a shared field and property associated with an instance of a class, you change the value associated with all instances of the class. In this way, shared fields and properties behave like global variables that can be accessed only from instances of a class. Without static fields and properties, you would need to use module-level variables to achieve the same effect. However, module-level variables can make your classes difficult to understand and maintain. Furthermore, using module-level variables in this way violates the concept of encapsulation that classes represent.
Shared procedures are class methods that are not associated with a specific instance of a class. For example, the Cos method defined within the Math class is a shared method. You can call a shared procedure by calling it either as a method of an object, or directly from the class. In this way, shared procedures represent an exception to the rule that you must create an instance of a class before you can use it.
Shared procedures are not implicitly passed instances of the class. For this reason, no unqualified references to non-shared data members are allowed in shared methods.
[/msdn]