Vb. NET Learning Notes--type conversion

Source: Internet
Author: User
1. Conversion function DirectCast: A type conversion operation based on inheritance or implementation. Ctype:trycast: When converting back and forth between Object data types, DirectCast does not use the Visual Basic run-time helper Routines for conversion, so it can provide better performance than CType. The DirectCast keyword is used in the same way as using the CType function and the TryCast keyword. Provides an expression as the first argument, providing a type to convert it to the second argument. DirectCast requires an inheritance or implementation relationship between data types of two parameters. This means that one type must inherit or implement another type. Errors and failures if DirectCast detects that no inheritance or implementation relationships exist, a compiler error is generated. But there is no compiler error and there is no guarantee that a successful conversion will occur. If the desired conversion is a narrowing conversion, it may fail at run time. If this condition occurs, the runtime throws a InvalidCastException error. The conversion keyword type conversion keyword is compared as follows. Keyword data type parameter relational Run-time failure CType function Any data type must define widening or narrowing conversions between two data types to raise InvalidCastException directcast any data type a type must inherit or implement another type throw InvalidCastException TryCast only reference type a type must inherit or implement another type to return nothing (Visual Basic) Example The following example shows two uses of DirectCast, one that fails at run time, Another success. Visual Basic Copy code Dim q as Object = 2.37 Dim i as Integer = CType (q, Integer) ' The following conversion fails at run time Dim J As Integer = DirectCast (q, Integer) Dim f As New System.Windows.Forms.Form Dim c As System.Windows.Forms.Control ' The following conversion succeeds. c = DirectCast (f, System.Windows.Forms.CoNtrol) In the above example, the run-time type of Q is Double. CType can succeed because Double can be converted to Integer. However, the first DirectCast fails at run time because the Run-time type of Double has no inheritance relationship with the Integer, even if it is possible to convert. The second DirectCast succeeds because it is converted from the form type to the control type, and the form inherits from that type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.