Readonly Property Cannot Be Used As An Assignment Target Department

Set Statement (Visual Basic)

Declares a property procedure used to assign a value to a property.

Syntax

Parts


Optional. See Attribute List.


Optional on at most one of the and statements in this property. Can be one of the following:

See Access levels in Visual Basic.


Required. Parameter containing the new value for the property.


Required if is . Data type of the parameter. The data type specified must be the same as the data type of the property where this statement is declared.


Optional. One or more statements that run when the property procedure is called.


Required. Terminates the definition of the property procedure.

Every property must have a property procedure unless the property is marked . The procedure is used to set the value of the property.

Visual Basic automatically calls a property's procedure when an assignment statement provides a value to be stored in the property.

Visual Basic passes a parameter to the procedure during property assignments. If you do not supply a parameter for , the integrated development environment (IDE) uses an implicit parameter named . The parameter holds the value to be assigned to the property. You typically store this value in a private local variable and return it whenever the procedure is called.

The body of the property declaration can contain only the property's and procedures between the Property Statement and the statement. It cannot store anything other than those procedures. In particular, it cannot store the property's current value. You must store this value outside the property, because if you store it inside either of the property procedures, the other property procedure cannot access it. The usual approach is to store the value in a Private variable declared at the same level as the property. You must define a procedure inside the property to which it applies.

The procedure defaults to the access level of its containing property unless you use in the statement.

Rules

  • Mixed Access Levels. If you are defining a read-write property, you can optionally specify a different access level for either the or the procedure, but not both. If you do this, the procedure access level must be more restrictive than the property's access level. For example, if the property is declared , you can declare the procedure , but not .

    If you are defining a property, the procedure represents the entire property. You cannot declare a different access level for , because that would set two access levels for the property.

Behavior

  • Returning from a Property Procedure. When the procedure returns to the calling code, execution continues following the statement that provided the value to be stored.

    property procedures can return using either the Return Statement or the Exit Statement.

    The and statements cause an immediate exit from a property procedure. Any number of and statements can appear anywhere in the procedure, and you can mix and statements.

Example

The following example uses the statement to set the value of a property.

See Also

Get Statement
Property Statement
Sub Statement
Property Procedures

How to: Define Abstract Properties (C# Programming Guide)

The following example shows how to define abstract properties. An abstract property declaration does not provide an implementation of the property accessors -- it declares that the class supports properties, but leaves the accessor implementation to derived classes. The following example demonstrates how to implement the abstract properties inherited from a base class.

This sample consists of three files, each of which is compiled individually and its resulting assembly is referenced by the next compilation:

  • abstractshape.cs: the class that contains an abstract property.

  • shapes.cs: The subclasses of the class.

  • shapetest.cs: A test program to display the areas of some -derived objects.

To compile the example, use the following command:

This will create the executable file shapetest.exe.

Example

This file declares the class that contains the property of the type .

  • Modifiers on the property are placed on the property declaration itself. For example:

  • When declaring an abstract property (such as in this example), you simply indicate what property accessors are available, but do not implement them. In this example, only a get accessor is available, so the property is read-only.

Example

The following code shows three subclasses of and how they override the property to provide their own implementation.

Example

The following code shows a test program that creates a number of -derived objects and prints out their areas.

See Also

C# Programming Guide
Classes and Structs
Abstract and Sealed Classes and Class Members
Properties
How to: Create and Use Assemblies Using the Command Line

0 thoughts on “Readonly Property Cannot Be Used As An Assignment Target Department

Leave a Reply

Your email address will not be published. Required fields are marked *