3.1. Member Variables
As shown in Example 3-1, member variables of a class are typically declared at the top of the class block (with or without an initial state).
The term "member variables" also includes constants and enumerations. By default, constants and enumerations are shared, so an instance of the class is not necessary; these values pertain to every instance of an object. Constants can refer to any data type, while enumerations are restricted to Byte, Short, Integer, and Long. Example 3-2 illustrates the definition of member variables in a class.
Example 3-2. Member variables
Public Class World 'Member data represents state Private age As Double 'Constants Public Const AverageDensity As Single = 5515 '(kg/m3) Public Const PolarRadius = 6356.8 'In km Public Const SatelliteCount As Byte = 1 'The Moon 'Enums Public Enum Continents Africa = 1 Antarctica = 2 Asia = 3 Australia = 4 Europe = 5 NorthAmerica = 6 SouthAmerica = 7 End Enum Public Shared Sub Hello( ) Console.WriteLine("Hello, World!") End Sub End Class |
Sidebar 1. Variable NamesWhen naming classes or constant values, by convention PascalCasing should be used. With PascalCasing, each word comprising the variable name should start with an uppercase letter. Conversely, instance variables use camelCasing, which always makes the first word lowercase. In all cases, Hungarian notation is avoided. Rather than naming your variables based on a type, you should use a meaningful name instead. The .NET Framework Design ... |
Get Object-Oriented Programming with Visual Basic .NET 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.