You must use CType when your code has the OPTION STRICT ON either in the
project properties or in the VB Code itself and when the conversion is a
narrowing one.
For instance, you can always write: Dim i as Integer
Dim d as Double = i
But if you have set "Option strict On" in the project properties or in the
code, Dim d as Double
Dim i as Integer = d will not
compile,
you have to write: Dim d as Double
Dim i as Integer = CType(d, double)
(it's a way to tell the compiler that you know what you are
doing)
For objects, if Customer Inherits from Person, you can always write: Dim C
as new Customer
Dim P as Person = C
But with Option Strict On, if you write Dim C as new Customer
Dim P as Person =
C
Dim D as Customer
= P, the compiler will not accept the last statement
it will accept: Dim C as new Customer
Dim P as Persomn = C
Dim D as Customer = CType(P, Customer)
I hope I am clear enough. Sometimes my students understand what I say.
Marc Biotteau, MCT
"winlin" <winlin (AT) verizon (DOT) com> wrote
Quote:
Hello
Under what conditions "must" you use "CTYPE" when working with variables
and
objects? |