संपादित करें

इसके माध्यम से साझा किया गया


Option Strict On disallows implicit conversions from '<type1>' to '<type2>'

You have tried to convert a type to another type that may not be able to contain the value, such as a Long to an Integer, while the type checking switch (Option Strict Statement) is set to On.

This type of conversion is called a narrowing conversion, and it is possible for it to fail at run time. For this reason, Option Strict On disallows implicit narrowing conversions.

Error ID: BC30512

To correct this error

  1. Determine whether a conversion of any type exists from <type1> to <type2>. If both are Visual Basic elementary types, or if both are instances of classes, you can usually make this determination by consulting the table in Widening and Narrowing Conversions.

  2. If only a narrowing conversion exists from <type1> to <type2>, you should use explicit casting. The CType Function and DirectCast Operator keywords throw a run-time exception if the conversion fails. The TryCast Operator keyword applies only to reference types and returns Nothing if the conversion fails.

  3. If a narrowing conversion exists and your program can tolerate a run-time failure, or you are confident that a run-time failure is not possible, you can specify Option Strict Off at the beginning of your source code. But you should still enclose the conversion in a Try...Catch...Finally Statement block to avoid unexpected results or early termination of your program.

  4. If no conversion exists from <type1> to <type2>, you must re-evaluate your program logic. You might be able to write code that can assign values to <type2> corresponding to anticipated values of <type1>.

  5. If no conversion exists from <type1> to <type2> and one of the types is a class or structure you have defined, you might be able to define a conversion operator from that type to or from the other type. For more information, see How to: Define a Conversion Operator.

  6. In all cases and as a general guideline, you should avoid using narrowing conversions unless you can trap failures in a Catch block and deal with them effectively.

See also