Edit

Share via


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

Variable '<variablename>' is passed by reference before it has been assigned a value. A null reference exception could result at run time.

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

If a variable has never been assigned a value, it holds the default value for its data type. For a reference data type, that default value is Nothing. Reading a reference variable 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, see Configuring Warnings in Visual Basic.

Error ID: BC42030

To correct this error

  • If you intend the procedure to assign a value to the variable through the ByRef argument, and if it does not matter whether the variable already holds a value, then no action is necessary.

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

  • If the logic in the procedure reads the argument before assigning any value to it, and if the variable 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 to catch a NullReferenceException.

See also