Differences Between Properties and Variables in Visual Basic
Variables and properties both represent values that you can access. However, there are differences in storage and implementation.
Variables
A variable corresponds directly to a memory location. You define a variable with a single declaration statement. A variable can be a local variable, defined inside a procedure and available only within that procedure, or it can be a member variable, defined in a module, class, or structure but not inside any procedure. A member variable is also called a field.
Properties
A property is a data element defined on a module, class, or structure. You define a property with a code block between the Property and End Property statements. The code block contains a Get procedure, a Set procedure, or both. These procedures are called property procedures or property accessors. In addition to retrieving or storing the property's value, they can also perform custom actions, such as updating an access counter.
Differences
The following table shows some important differences between variables and properties.
Point of difference |
Variable |
Property |
---|---|---|
Declaration |
Single declaration statement |
Series of statements in a code block |
Implementation |
Single storage location |
Executable code (property procedures) |
Storage |
Directly associated with variable's value |
Typically has internal storage not available outside the property's containing class or module Property's value might or might not exist as a stored element 1 |
Executable code |
None |
Must have at least one procedure |
Read and write access |
Read/write or read-only |
Read/write, read-only, or write-only |
Custom actions (in addition to accepting or returning value) |
Not possible |
Can be performed as part of setting or retrieving property value |
1 Unlike a variable, the value of a property might not correspond directly to a single item of storage. The storage might be split into pieces for convenience or security, or the value might be stored in an encrypted form. In these cases the Get procedure would assemble the pieces or decrypt the stored value, and the Set procedure would encrypt the new value or split it into the constituent storage. A property value might be ephemeral, like time of day, in which case the Get procedure would calculate it on the fly each time you access the property.
See Also
Tasks
How to: Declare a Property with Mixed Access Levels
How to: Call a Property Procedure
How to: Declare and Call a Default Property in Visual Basic
How to: Put a Value in a Property
How to: Get a Value from a Property
Concepts
Procedure Parameters and Arguments