Share via


Variable '<variablename>' is passed by reference before it has been assigned a value (Structure Variable)

Variable '<variablename>' is passed by reference before it has been assigned a value. A null reference exception could result at runtime. Make sure the structure or all the reference members are initialized before use

A procedure call passes a structure variable as an argument to a ByRef parameter before any value is assigned to the variable.

If a structure variable has never been assigned a value, each structure member holds the default value for its data type. For a reference data type, that default value is Nothing (Visual Basic). Reading a reference member that has a value of Nothing can cause a NullReferenceException in some circumstances.

Passing an argument to a procedure ByRef exposes the variable underlying the argument to possible modification by the procedure.

By default, this message is a warning. For more information about hiding warnings or treating warnings as errors, please see Configuring Warnings in Visual Basic.

Error ID: BC42108

To correct this error

  • If you intend the procedure to assign values to structure members through the ByRef argument, and if it does not matter whether the members already hold values, then no action is necessary.

  • If the logic in the procedure reads a structure member before assigning any value to it, and if the member is of a value type, then make sure that the procedure logic does not depend on whether the member holds its default value or not.

  • If the logic in the procedure reads a structure member before assigning any value to it, and if the member is of a reference type, then make sure that the procedure logic can handle a value of Nothing. For example, it could use a Try...Catch...Finally Statement (Visual Basic) to catch a NullReferenceException.

See Also

Tasks

Troubleshooting Variables in Visual Basic

Concepts

Value Types and Reference Types

Passing Arguments by Value and by Reference

Variable Declaration in Visual Basic

Reference

Dim Statement (Visual Basic)

ByRef

Structure Statement

Other Resources

Structures: Your Own Data Types