Type conversion

xiaoxiao2021-03-06  67

LZMTWHTTP: //Blog.9cbs.net/lzmtw2004-11-21 In VB.NET, there are two main occasions involving type conversion:

First, the basic type of mutual conversion,

The second is to convert an expression into an object, structure, class, or interface.

First, the basic type of mutual conversion

There are two types of data conversions here: extended and shrinkage.

Extension: The conversion can maintain the original data value, no data loss.

Shrink: From a large type to a smaller type to convert data (by byte or accuracy), the small data type cannot maintain the original value.

Application method:

1, Sytem.Convert class

The supported base type is Boolean, CHAR, SBYTE, BYTE, INT16, INT32, INT64, UINT16, UINT32, UINT64, SINGLE, DOUBLE, DECIMAL, DATETIME, and STRING.

2, internal type conversion function

CBOOL (Expression)

CBYTE (Expression)

Cchar (Expression)

CDATE (Expression)

CDBL (Expression)

CDEC (Expression)

CINT (Expression)

CLNG (Expression)

COBJ (Expression)

Cshort (Expression)

CSNG (Expression)

CSTR (Expression)

3, NET value analysis characteristics: PARSE method

The PARSE method is a member of the NumberStyles enumeration, which is survived in the System.Globalization namespace.

Second, convert an expression into objects, structures, classes, or interfaces

Application method:

CType keyword

Directcast keyword

Both can return expressions explicitly convert to specified data types, objects, structures, classes, or interfaces.

Both keywords will be converted as the first parameter, and the type to be converted into the second parameter. These two conversions will fail if the data type of the data type and the data type specified by the second parameter is not defined.

Please be aware of:

1. As long as the effective conversion between the expression and the type is defined, CType can succeed, while DirectCast requires the runtime type of the object variable as the specified type. The special manifestation of Directcast is specifically in: From the Object Type to any other type of conversion as a direct forced conversion along the hierarchical structure, ignore all of the special conversion behavior in Object. When converting the runtime type of Object type Object type Object type, if the specified type is different from the runtime type of the expression, DirectCast will trigger the System.INValidTyPeException. However, if the specified type of the expression is the same, the runtime performance of Directcast is better than the runtime performance of CType.

DIM Q as Object = 2.37 'Requires option strict to be off.

DIM I as integer = ctype (q, integer) 'succeeds.

DIM J AS INTEGER = Directcast (q, integer) 'Fails.

In the above example, the runtime type of Q is Double. CType is successful because Double can be converted to Integer, DirectCast failed because the runtime type of Q is not Integer.

2, DirectCast only converts the reference type variable, does not convert the value type variable

DIM Q as integer = -1

DIM I as boolean = ctype (q, boolean) 'succeeds.dim j AS boolean = Directcast (q, boolean)' Fails.

Attachment: About value type and reference type

If a data type contains data in its own memory allocation, the data type is "value type". The "Reference Type" contains a pointer to other memory locations that contain data.

Value types include:

All digital data types

Boolean, Char and Date

All structures, even if its members are reference types

Enumeration because its basic type is always Byte, Short, Integer or Long

The reference types include:

String

All arrays, even their elements are value types

Class type, such as Form

Delegate

The reference type or value type can be assigned to the variable of the Object data type. The Object variable always holds a pointer to the data, never hold the data itself. However, if the value type is assigned to the Object variable, the Object variable will behave like holding your own data.

By passing an Object variable to the ISReference method of the Information class in the Microsoft.visualBasic namespace, you can see that the variable acts as a reference type or a value type. If the content of the Object variable indicates a reference type, Microsoft.VisualBasic.information.IsReference returns true.

转载请注明原文地址:https://www.9cbs.com/read-89173.html

New Post(0)