[Translation] Visual Basic 2005 Enhancements in Language (6) Isnot operator and TRYCAST statement

xiaoxiao2021-03-06  35

Friendly reminds, there is a mistake in translation of this article, please be sure to combine comments to see this translation! ISNOT operator

Isnot is

The antisense operator of IS. Before you reference an object, you must check if it has been initialized, and in the past, we have used this way to use this very important way:

IF

Not myobj

Is nothing then

IsNot allows you to do direct comparison, else to use

NOT operator's troubles:

IF myobj

ISNOT

Nothing then

Similarly, you don't have to use two object instances or if you do not use it.

NOT operator:

IF myobj1

ISNOT MYOBJ2

THEN

Don't see this is just a simple change.

IsNot makes up for a shortcoming of Visual Basic language consistency, your code will become more concise.

TRYCAST statement

In Visual Basic 2003, you can use the following two methods to convert the type of object to another type:

DIM P

As products

P =

CType (Obj, Product)

P =

Directcast (Obj, Product)

Above, you are using

CType converts a type to another, and

DirectCast requires that there is an inheritance or implementation of the interface between the two objects (the instructions here)

mistaken:

CType and

The actual difference between Directcast is

DirectCast requires the same type of time when the object is the same as the specified conversion type, and

The operating efficiency of CType is slightly lower, and 淌 注). But the problem here is that no matter which method you use, if the OBJ cannot be converted or cast (the conversion is "convert", the cast is "cast", the author confuses the difference between Convert and Cast in some places, in fact, their difference Be

CType and

Directcast difference. But the author believes that there is no need to go to this word, so I am unified in other places to "convert", 淌 注) as a product type, then the runtime will result in an exception.

Use new

TRYCAST statement. It and

CType or

The way Directcast is the same, but it will return if the type conversion cannot be completed.

Nothing value to the object:

P =

Trycast (Obj, Product)

IF P

ISNOT Nothing

THEN

'Use P objects ...

END IF

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

New Post(0)