Udostępnij za pośrednictwem


Property Procedures vs. Fields

Properties and fields both store and retrieve information in an object. Their similarity can make it difficult to determine which is a better programming choice in a given case.

Use property procedures when:

  • You need to control when and how a value is set or retrieved.

  • The property has a well-defined set of values that need to be validated.

  • Setting the value causes some perceptible change in the object's state, such as an IsVisible property.

  • Setting the property causes changes to other internal variables or to the values of other properties.

  • A set of steps must be performed before the property can be set or retrieved.

Use fields when:

  • The value is of a self-validating type. For example, an error or automatic data conversion occurs if a value other than True or False is assigned to a Boolean variable.

  • Any value in the range supported by the data type is valid. This is true of many properties of type Single or Double.

  • The property is a String data type, and there is no constraint on the size or value of the string.

See Also

Concepts

Classes vs. Modules

Properties and Property Procedures

Data Types in Visual Basic