Declaring Shared Fields

Fields are variables defined in classes and structures. (Effectively, the use of data in modules is roughly equivalent to the use of shared fields in structures and classes.)

To define a shared field, precede the field declaration with the desired access specifier, the Shared keyword, the variable name, and the data type. Include an initializer if it's appropriate to do so. The following statement demonstrates declaring a shared integer:

Public Shared Counter As Long = 0

No matter how few or how many instances of the objects there are containing the preceding statement, there will only ever be exactly one Counter field for that class. Contained in a class, Counter may appear this way:

 Public Class SharedInteger Public ...

Get Visual Basic® .NET Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.